2011-10-10 09:38:02 +00:00
|
|
|
/*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
|
*
|
2011-10-10 09:38:02 +00:00
|
|
|
* Contributor(s): Reevan McKay
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
|
2011-02-27 20:40:57 +00:00
|
|
|
/** \file blender/blenkernel/intern/deform.c
|
|
|
|
|
* \ingroup bke
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include <string.h>
|
2004-11-06 21:59:35 +00:00
|
|
|
#include <math.h>
|
2012-04-06 04:46:47 +00:00
|
|
|
#include <ctype.h>
|
2012-09-05 04:16:09 +00:00
|
|
|
#include <stdlib.h>
|
2012-12-28 01:36:00 +00:00
|
|
|
#include <stddef.h>
|
2004-09-14 19:03:11 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
2004-09-14 19:03:11 +00:00
|
|
|
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
Transfer Data: add main core code and operators.
This add code needed to map a CD data layout from source mesh towards destination one,
and code needed to actually transfer data, using BKE's mesh remap generated data.
This allows to transfer most CD layers (vgroups, vcols, uvs...) as well as fake, boolean ones
(like smooth/sharp edges/faces, etc.). Some types are not yet transferable, mainly
shape keys, this is known TODO.
Data transfer can also use some advanced mixing in some cases (mostly, vgroups and vcols).
Notes:
* New transfer operators transfer data from active object towards selected ones.
* Modifier will be committed separately.
* Old weight transfer code (for vgroups) is kept for now, mostly because it is the only
usable one in weightpaint mode (it transfers from selected object to active one,
this is not sensible in Object mode, but needed in WeightPaint one). This will be addressed soon.
Again, heavily reviewed and enhanced by Campbell, thanks!
2015-01-09 19:11:40 +01:00
|
|
|
#include "DNA_mesh_types.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "DNA_object_types.h"
|
Transfer Data: add main core code and operators.
This add code needed to map a CD data layout from source mesh towards destination one,
and code needed to actually transfer data, using BKE's mesh remap generated data.
This allows to transfer most CD layers (vgroups, vcols, uvs...) as well as fake, boolean ones
(like smooth/sharp edges/faces, etc.). Some types are not yet transferable, mainly
shape keys, this is known TODO.
Data transfer can also use some advanced mixing in some cases (mostly, vgroups and vcols).
Notes:
* New transfer operators transfer data from active object towards selected ones.
* Modifier will be committed separately.
* Old weight transfer code (for vgroups) is kept for now, mostly because it is the only
usable one in weightpaint mode (it transfers from selected object to active one,
this is not sensible in Object mode, but needed in WeightPaint one). This will be addressed soon.
Again, heavily reviewed and enhanced by Campbell, thanks!
2015-01-09 19:11:40 +01:00
|
|
|
#include "DNA_scene_types.h"
|
2004-09-14 19:03:11 +00:00
|
|
|
|
2012-09-05 04:16:09 +00:00
|
|
|
#include "BLI_listbase.h"
|
|
|
|
|
#include "BLI_math.h"
|
|
|
|
|
#include "BLI_path_util.h"
|
|
|
|
|
#include "BLI_string.h"
|
2011-10-27 12:37:14 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2013-03-25 08:29:06 +00:00
|
|
|
#include "BLF_translation.h"
|
|
|
|
|
|
Transfer Data: add main core code and operators.
This add code needed to map a CD data layout from source mesh towards destination one,
and code needed to actually transfer data, using BKE's mesh remap generated data.
This allows to transfer most CD layers (vgroups, vcols, uvs...) as well as fake, boolean ones
(like smooth/sharp edges/faces, etc.). Some types are not yet transferable, mainly
shape keys, this is known TODO.
Data transfer can also use some advanced mixing in some cases (mostly, vgroups and vcols).
Notes:
* New transfer operators transfer data from active object towards selected ones.
* Modifier will be committed separately.
* Old weight transfer code (for vgroups) is kept for now, mostly because it is the only
usable one in weightpaint mode (it transfers from selected object to active one,
this is not sensible in Object mode, but needed in WeightPaint one). This will be addressed soon.
Again, heavily reviewed and enhanced by Campbell, thanks!
2015-01-09 19:11:40 +01:00
|
|
|
#include "BKE_customdata.h"
|
|
|
|
|
#include "BKE_object_data_transfer.h"
|
2013-02-22 04:09:04 +00:00
|
|
|
#include "BKE_deform.h" /* own include */
|
Transfer Data: add main core code and operators.
This add code needed to map a CD data layout from source mesh towards destination one,
and code needed to actually transfer data, using BKE's mesh remap generated data.
This allows to transfer most CD layers (vgroups, vcols, uvs...) as well as fake, boolean ones
(like smooth/sharp edges/faces, etc.). Some types are not yet transferable, mainly
shape keys, this is known TODO.
Data transfer can also use some advanced mixing in some cases (mostly, vgroups and vcols).
Notes:
* New transfer operators transfer data from active object towards selected ones.
* Modifier will be committed separately.
* Old weight transfer code (for vgroups) is kept for now, mostly because it is the only
usable one in weightpaint mode (it transfers from selected object to active one,
this is not sensible in Object mode, but needed in WeightPaint one). This will be addressed soon.
Again, heavily reviewed and enhanced by Campbell, thanks!
2015-01-09 19:11:40 +01:00
|
|
|
#include "BKE_mesh_mapping.h"
|
|
|
|
|
#include "BKE_object_deform.h"
|
|
|
|
|
|
|
|
|
|
#include "data_transfer_intern.h"
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2013-07-04 03:56:18 +00:00
|
|
|
bDeformGroup *BKE_defgroup_new(Object *ob, const char *name)
|
|
|
|
|
{
|
|
|
|
|
bDeformGroup *defgroup;
|
|
|
|
|
|
|
|
|
|
BLI_assert(OB_TYPE_SUPPORT_VGROUP(ob->type));
|
|
|
|
|
|
|
|
|
|
defgroup = MEM_callocN(sizeof(bDeformGroup), __func__);
|
|
|
|
|
|
|
|
|
|
BLI_strncpy(defgroup->name, name, sizeof(defgroup->name));
|
|
|
|
|
|
|
|
|
|
BLI_addtail(&ob->defbase, defgroup);
|
|
|
|
|
defgroup_unique_name(defgroup, ob);
|
|
|
|
|
|
|
|
|
|
return defgroup;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
void defgroup_copy_list(ListBase *outbase, ListBase *inbase)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
bDeformGroup *defgroup, *defgroupn;
|
|
|
|
|
|
2014-02-08 06:07:10 +11:00
|
|
|
BLI_listbase_clear(outbase);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
for (defgroup = inbase->first; defgroup; defgroup = defgroup->next) {
|
|
|
|
|
defgroupn = defgroup_duplicate(defgroup);
|
2002-10-12 11:37:38 +00:00
|
|
|
BLI_addtail(outbase, defgroupn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
bDeformGroup *defgroup_duplicate(bDeformGroup *ingroup)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
bDeformGroup *outgroup;
|
|
|
|
|
|
|
|
|
|
if (!ingroup)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
outgroup = MEM_callocN(sizeof(bDeformGroup), "copy deformGroup");
|
2011-11-30 07:39:54 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/* For now, just copy everything over. */
|
2012-03-20 04:09:43 +00:00
|
|
|
memcpy(outgroup, ingroup, sizeof(bDeformGroup));
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
outgroup->next = outgroup->prev = NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
|
return outgroup;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-23 15:37:08 +00:00
|
|
|
/* overwrite weights filtered by vgroup_subset
|
|
|
|
|
* - do nothing if neither are set.
|
|
|
|
|
* - add destination weight if needed
|
|
|
|
|
*/
|
|
|
|
|
void defvert_copy_subset(MDeformVert *dvert_dst, const MDeformVert *dvert_src,
|
|
|
|
|
const bool *vgroup_subset, const int vgroup_tot)
|
|
|
|
|
{
|
|
|
|
|
int defgroup;
|
2013-06-25 10:44:30 +00:00
|
|
|
for (defgroup = 0; defgroup < vgroup_tot; defgroup++) {
|
2013-06-23 15:37:08 +00:00
|
|
|
if (vgroup_subset[defgroup]) {
|
|
|
|
|
defvert_copy_index(dvert_dst, dvert_src, defgroup);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
void defvert_copy(MDeformVert *dvert_dst, const MDeformVert *dvert_src)
|
2010-01-15 17:28:00 +00:00
|
|
|
{
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dvert_dst->totweight == dvert_src->totweight) {
|
|
|
|
|
if (dvert_src->totweight)
|
|
|
|
|
memcpy(dvert_dst->dw, dvert_src->dw, dvert_src->totweight * sizeof(MDeformWeight));
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dvert_dst->dw)
|
|
|
|
|
MEM_freeN(dvert_dst->dw);
|
2010-01-15 17:28:00 +00:00
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dvert_src->totweight)
|
2012-03-20 04:09:43 +00:00
|
|
|
dvert_dst->dw = MEM_dupallocN(dvert_src->dw);
|
2010-01-15 17:28:00 +00:00
|
|
|
else
|
2012-03-20 04:09:43 +00:00
|
|
|
dvert_dst->dw = NULL;
|
2010-01-15 17:28:00 +00:00
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
dvert_dst->totweight = dvert_src->totweight;
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 07:11:29 +00:00
|
|
|
/* copy an index from one dvert to another
|
|
|
|
|
* - do nothing if neither are set.
|
|
|
|
|
* - add destination weight if needed.
|
|
|
|
|
*/
|
2011-11-30 07:39:54 +00:00
|
|
|
void defvert_copy_index(MDeformVert *dvert_dst, const MDeformVert *dvert_src, const int defgroup)
|
2011-11-30 07:11:29 +00:00
|
|
|
{
|
|
|
|
|
MDeformWeight *dw_src, *dw_dst;
|
|
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_src = defvert_find_index(dvert_src, defgroup);
|
2011-11-30 07:11:29 +00:00
|
|
|
|
|
|
|
|
if (dw_src) {
|
|
|
|
|
/* source is valid, verify destination */
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_dst = defvert_verify_index(dvert_dst, defgroup);
|
|
|
|
|
dw_dst->weight = dw_src->weight;
|
2011-11-30 07:11:29 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* source was NULL, assign zero, could also remove */
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_dst = defvert_find_index(dvert_dst, defgroup);
|
2011-11-30 07:11:29 +00:00
|
|
|
|
|
|
|
|
if (dw_dst) {
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_dst->weight = 0.0f;
|
2011-11-30 07:11:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 17:07:47 +00:00
|
|
|
/* only sync over matching weights, don't add or remove groups
|
|
|
|
|
* warning, loop within loop.
|
|
|
|
|
*/
|
2013-03-20 23:14:18 +00:00
|
|
|
void defvert_sync(MDeformVert *dvert_dst, const MDeformVert *dvert_src, const bool use_verify)
|
2010-01-26 17:07:47 +00:00
|
|
|
{
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dvert_src->totweight && dvert_dst->totweight) {
|
2010-01-26 17:07:47 +00:00
|
|
|
int i;
|
2011-11-30 07:39:54 +00:00
|
|
|
MDeformWeight *dw_src;
|
2012-03-20 04:09:43 +00:00
|
|
|
for (i = 0, dw_src = dvert_src->dw; i < dvert_src->totweight; i++, dw_src++) {
|
2011-11-30 07:39:54 +00:00
|
|
|
MDeformWeight *dw_dst;
|
2012-03-20 04:09:43 +00:00
|
|
|
if (use_verify) dw_dst = defvert_verify_index(dvert_dst, dw_src->def_nr);
|
|
|
|
|
else dw_dst = defvert_find_index(dvert_dst, dw_src->def_nr);
|
2011-11-30 07:39:54 +00:00
|
|
|
|
|
|
|
|
if (dw_dst) {
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_dst->weight = dw_src->weight;
|
2010-01-26 17:07:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* be sure all flip_map values are valid */
|
2011-11-30 07:39:54 +00:00
|
|
|
void defvert_sync_mapped(MDeformVert *dvert_dst, const MDeformVert *dvert_src,
|
2013-03-20 23:14:18 +00:00
|
|
|
const int *flip_map, const int flip_map_len, const bool use_verify)
|
2010-01-26 17:07:47 +00:00
|
|
|
{
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dvert_src->totweight && dvert_dst->totweight) {
|
2010-01-26 17:07:47 +00:00
|
|
|
int i;
|
2011-11-30 07:39:54 +00:00
|
|
|
MDeformWeight *dw_src;
|
2012-03-20 04:09:43 +00:00
|
|
|
for (i = 0, dw_src = dvert_src->dw; i < dvert_src->totweight; i++, dw_src++) {
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dw_src->def_nr < flip_map_len) {
|
|
|
|
|
MDeformWeight *dw_dst;
|
2012-03-20 04:09:43 +00:00
|
|
|
if (use_verify) dw_dst = defvert_verify_index(dvert_dst, flip_map[dw_src->def_nr]);
|
|
|
|
|
else dw_dst = defvert_find_index(dvert_dst, flip_map[dw_src->def_nr]);
|
2011-11-30 07:39:54 +00:00
|
|
|
|
|
|
|
|
if (dw_dst) {
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_dst->weight = dw_src->weight;
|
2011-10-27 07:54:32 +00:00
|
|
|
}
|
2010-01-26 17:07:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-02 21:43:26 +00:00
|
|
|
/* be sure all flip_map values are valid */
|
2011-12-09 08:20:27 +00:00
|
|
|
void defvert_remap(MDeformVert *dvert, int *map, const int map_len)
|
2010-02-02 21:43:26 +00:00
|
|
|
{
|
2012-03-20 04:09:43 +00:00
|
|
|
MDeformWeight *dw = dvert->dw;
|
2011-12-14 21:08:08 +00:00
|
|
|
unsigned int i;
|
2012-03-20 04:09:43 +00:00
|
|
|
for (i = dvert->totweight; i != 0; i--, dw++) {
|
2011-12-09 08:20:27 +00:00
|
|
|
if (dw->def_nr < map_len) {
|
2012-03-20 04:09:43 +00:00
|
|
|
dw->def_nr = map[dw->def_nr];
|
2011-12-14 21:08:08 +00:00
|
|
|
|
2012-03-09 00:41:09 +00:00
|
|
|
/* just in case */
|
2011-12-14 21:08:08 +00:00
|
|
|
BLI_assert(dw->def_nr >= 0);
|
2011-12-09 08:20:27 +00:00
|
|
|
}
|
2010-02-02 21:43:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-23 15:37:08 +00:00
|
|
|
/**
|
|
|
|
|
* Same as #defvert_normalize but takes a bool array.
|
|
|
|
|
*/
|
|
|
|
|
void defvert_normalize_subset(MDeformVert *dvert,
|
|
|
|
|
const bool *vgroup_subset, const int vgroup_tot)
|
|
|
|
|
{
|
2013-10-10 06:33:23 +00:00
|
|
|
if (dvert->totweight == 0) {
|
|
|
|
|
/* nothing */
|
|
|
|
|
}
|
|
|
|
|
else if (dvert->totweight == 1) {
|
|
|
|
|
MDeformWeight *dw = dvert->dw;
|
2013-06-23 15:37:08 +00:00
|
|
|
if ((dw->def_nr < vgroup_tot) && vgroup_subset[dw->def_nr]) {
|
2013-10-10 06:33:23 +00:00
|
|
|
dw->weight = 1.0f;
|
2013-06-23 15:37:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-10-10 06:33:23 +00:00
|
|
|
else {
|
|
|
|
|
MDeformWeight *dw;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
float tot_weight = 0.0f;
|
2013-06-23 15:37:08 +00:00
|
|
|
|
|
|
|
|
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
|
|
|
|
|
if ((dw->def_nr < vgroup_tot) && vgroup_subset[dw->def_nr]) {
|
2013-10-10 06:33:23 +00:00
|
|
|
tot_weight += dw->weight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tot_weight > 0.0f) {
|
|
|
|
|
float scalar = 1.0f / tot_weight;
|
|
|
|
|
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
|
|
|
|
|
if ((dw->def_nr < vgroup_tot) && vgroup_subset[dw->def_nr]) {
|
|
|
|
|
dw->weight *= scalar;
|
|
|
|
|
|
|
|
|
|
/* in case of division errors with very low weights */
|
|
|
|
|
CLAMP(dw->weight, 0.0f, 1.0f);
|
|
|
|
|
}
|
2013-06-23 15:37:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
void defvert_normalize(MDeformVert *dvert)
|
2010-01-25 23:12:02 +00:00
|
|
|
{
|
2013-10-10 06:33:23 +00:00
|
|
|
if (dvert->totweight == 0) {
|
2010-01-25 23:12:02 +00:00
|
|
|
/* nothing */
|
|
|
|
|
}
|
2012-03-20 04:09:43 +00:00
|
|
|
else if (dvert->totweight == 1) {
|
|
|
|
|
dvert->dw[0].weight = 1.0f;
|
2010-01-25 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
MDeformWeight *dw;
|
2011-12-14 21:08:08 +00:00
|
|
|
unsigned int i;
|
2012-03-20 04:09:43 +00:00
|
|
|
float tot_weight = 0.0f;
|
2011-12-14 21:08:08 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
|
2011-12-14 21:08:08 +00:00
|
|
|
tot_weight += dw->weight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tot_weight > 0.0f) {
|
2012-03-20 04:09:43 +00:00
|
|
|
float scalar = 1.0f / tot_weight;
|
|
|
|
|
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
|
2011-12-14 21:08:08 +00:00
|
|
|
dw->weight *= scalar;
|
|
|
|
|
|
2012-03-09 00:41:09 +00:00
|
|
|
/* in case of division errors with very low weights */
|
2011-12-14 21:08:08 +00:00
|
|
|
CLAMP(dw->weight, 0.0f, 1.0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-10 06:33:23 +00:00
|
|
|
/* Same as defvert_normalize() if the locked vgroup is not a member of the subset */
|
|
|
|
|
void defvert_normalize_lock_single(MDeformVert *dvert,
|
|
|
|
|
const bool *vgroup_subset, const int vgroup_tot,
|
|
|
|
|
const int def_nr_lock)
|
2011-12-14 21:08:08 +00:00
|
|
|
{
|
2013-10-10 06:33:23 +00:00
|
|
|
if (dvert->totweight == 0) {
|
2011-12-14 21:08:08 +00:00
|
|
|
/* nothing */
|
|
|
|
|
}
|
2012-03-20 04:09:43 +00:00
|
|
|
else if (dvert->totweight == 1) {
|
2013-10-10 06:33:23 +00:00
|
|
|
MDeformWeight *dw = dvert->dw;
|
|
|
|
|
if ((dw->def_nr < vgroup_tot) && vgroup_subset[dw->def_nr]) {
|
|
|
|
|
if (def_nr_lock != 0) {
|
|
|
|
|
dw->weight = 1.0f;
|
|
|
|
|
}
|
2012-09-05 03:45:32 +00:00
|
|
|
}
|
2011-12-14 21:08:08 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2011-12-17 16:22:08 +00:00
|
|
|
MDeformWeight *dw_lock = NULL;
|
2011-12-14 21:08:08 +00:00
|
|
|
MDeformWeight *dw;
|
|
|
|
|
unsigned int i;
|
2012-03-20 04:09:43 +00:00
|
|
|
float tot_weight = 0.0f;
|
|
|
|
|
float lock_iweight = 1.0f;
|
2011-12-14 21:08:08 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
|
2013-10-10 06:33:23 +00:00
|
|
|
if ((dw->def_nr < vgroup_tot) && vgroup_subset[dw->def_nr]) {
|
|
|
|
|
if (dw->def_nr != def_nr_lock) {
|
|
|
|
|
tot_weight += dw->weight;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
dw_lock = dw;
|
|
|
|
|
lock_iweight = (1.0f - dw_lock->weight);
|
|
|
|
|
CLAMP(lock_iweight, 0.0f, 1.0f);
|
|
|
|
|
}
|
2011-12-14 21:08:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-25 23:12:02 +00:00
|
|
|
|
2011-12-14 21:08:08 +00:00
|
|
|
if (tot_weight > 0.0f) {
|
2012-03-09 00:41:09 +00:00
|
|
|
/* paranoid, should be 1.0 but in case of float error clamp anyway */
|
2011-12-14 21:08:08 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
float scalar = (1.0f / tot_weight) * lock_iweight;
|
|
|
|
|
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
|
2013-10-10 06:33:23 +00:00
|
|
|
if ((dw->def_nr < vgroup_tot) && vgroup_subset[dw->def_nr]) {
|
|
|
|
|
if (dw != dw_lock) {
|
|
|
|
|
dw->weight *= scalar;
|
2011-12-14 21:08:08 +00:00
|
|
|
|
2013-10-10 06:33:23 +00:00
|
|
|
/* in case of division errors with very low weights */
|
|
|
|
|
CLAMP(dw->weight, 0.0f, 1.0f);
|
|
|
|
|
}
|
2011-12-14 21:08:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-25 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-10 06:33:23 +00:00
|
|
|
/* Same as defvert_normalize() if no locked vgroup is a member of the subset */
|
2013-11-17 22:00:57 +11:00
|
|
|
void defvert_normalize_lock_map(
|
|
|
|
|
MDeformVert *dvert,
|
|
|
|
|
const bool *vgroup_subset, const int vgroup_tot,
|
|
|
|
|
const bool *lock_flags, const int defbase_tot)
|
2012-09-05 04:16:09 +00:00
|
|
|
{
|
2013-10-10 06:33:23 +00:00
|
|
|
if (dvert->totweight == 0) {
|
2012-09-05 04:16:09 +00:00
|
|
|
/* nothing */
|
|
|
|
|
}
|
|
|
|
|
else if (dvert->totweight == 1) {
|
2013-10-10 06:33:23 +00:00
|
|
|
MDeformWeight *dw = dvert->dw;
|
|
|
|
|
if ((dw->def_nr < vgroup_tot) && vgroup_subset[dw->def_nr]) {
|
|
|
|
|
if (LIKELY(defbase_tot >= 1) && lock_flags[0]) {
|
|
|
|
|
dw->weight = 1.0f;
|
|
|
|
|
}
|
2012-09-05 04:16:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
MDeformWeight *dw;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
float tot_weight = 0.0f;
|
|
|
|
|
float lock_iweight = 0.0f;
|
|
|
|
|
|
|
|
|
|
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
|
2013-10-10 06:33:23 +00:00
|
|
|
if ((dw->def_nr < vgroup_tot) && vgroup_subset[dw->def_nr]) {
|
2014-04-01 11:34:00 +11:00
|
|
|
if ((dw->def_nr < defbase_tot) && (lock_flags[dw->def_nr] == false)) {
|
2013-10-10 06:33:23 +00:00
|
|
|
tot_weight += dw->weight;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* invert after */
|
|
|
|
|
lock_iweight += dw->weight;
|
|
|
|
|
}
|
2012-09-05 04:16:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-23 13:28:22 +00:00
|
|
|
lock_iweight = max_ff(0.0f, 1.0f - lock_iweight);
|
2012-09-05 04:16:09 +00:00
|
|
|
|
|
|
|
|
if (tot_weight > 0.0f) {
|
|
|
|
|
/* paranoid, should be 1.0 but in case of float error clamp anyway */
|
|
|
|
|
|
|
|
|
|
float scalar = (1.0f / tot_weight) * lock_iweight;
|
|
|
|
|
for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
|
2013-10-10 06:33:23 +00:00
|
|
|
if ((dw->def_nr < vgroup_tot) && vgroup_subset[dw->def_nr]) {
|
2014-04-01 11:34:00 +11:00
|
|
|
if ((dw->def_nr < defbase_tot) && (lock_flags[dw->def_nr] == false)) {
|
2013-10-10 06:33:23 +00:00
|
|
|
dw->weight *= scalar;
|
2012-09-05 04:16:09 +00:00
|
|
|
|
2013-10-10 06:33:23 +00:00
|
|
|
/* in case of division errors with very low weights */
|
|
|
|
|
CLAMP(dw->weight, 0.0f, 1.0f);
|
|
|
|
|
}
|
2012-09-05 04:16:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
void defvert_flip(MDeformVert *dvert, const int *flip_map, const int flip_map_len)
|
2010-01-15 17:28:00 +00:00
|
|
|
{
|
|
|
|
|
MDeformWeight *dw;
|
|
|
|
|
int i;
|
|
|
|
|
|
2012-05-12 19:18:02 +00:00
|
|
|
for (dw = dvert->dw, i = 0; i < dvert->totweight; dw++, i++) {
|
2011-12-09 08:20:27 +00:00
|
|
|
if (dw->def_nr < flip_map_len) {
|
|
|
|
|
if (flip_map[dw->def_nr] >= 0) {
|
2012-03-20 04:09:43 +00:00
|
|
|
dw->def_nr = flip_map[dw->def_nr];
|
2011-12-09 08:20:27 +00:00
|
|
|
}
|
2011-10-27 07:54:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-19 21:09:16 +00:00
|
|
|
void defvert_flip_merged(MDeformVert *dvert, const int *flip_map, const int flip_map_len)
|
|
|
|
|
{
|
2012-03-20 03:48:32 +00:00
|
|
|
MDeformWeight *dw, *dw_cpy;
|
2012-03-19 21:09:16 +00:00
|
|
|
float weight;
|
|
|
|
|
int i, totweight = dvert->totweight;
|
|
|
|
|
|
|
|
|
|
/* copy weights */
|
2012-03-20 04:09:43 +00:00
|
|
|
for (dw = dvert->dw, i = 0; i < totweight; dw++, i++) {
|
2012-03-19 21:09:16 +00:00
|
|
|
if (dw->def_nr < flip_map_len) {
|
|
|
|
|
if (flip_map[dw->def_nr] >= 0) {
|
2012-03-20 03:48:32 +00:00
|
|
|
/* error checkers complain of this but we'll never get NULL return */
|
|
|
|
|
dw_cpy = defvert_verify_index(dvert, flip_map[dw->def_nr]);
|
|
|
|
|
dw = &dvert->dw[i]; /* in case array got realloced */
|
2012-03-19 21:09:16 +00:00
|
|
|
|
|
|
|
|
/* distribute weights: if only one of the vertex groups was
|
2012-05-16 23:37:23 +00:00
|
|
|
* assigned this will halve the weights, otherwise it gets
|
|
|
|
|
* evened out. this keeps it proportional to other groups */
|
2012-03-20 03:48:32 +00:00
|
|
|
weight = 0.5f * (dw_cpy->weight + dw->weight);
|
|
|
|
|
dw_cpy->weight = weight;
|
|
|
|
|
dw->weight = weight;
|
2012-03-19 21:09:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-15 17:28:00 +00:00
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
bDeformGroup *defgroup_find_name(Object *ob, const char *name)
|
2005-01-12 23:57:33 +00:00
|
|
|
{
|
2012-12-28 01:36:00 +00:00
|
|
|
return BLI_findstring(&ob->defbase, name, offsetof(bDeformGroup, name));
|
2005-01-12 23:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
int defgroup_name_index(Object *ob, const char *name)
|
2007-08-17 11:23:48 +00:00
|
|
|
{
|
2013-01-02 22:14:40 +00:00
|
|
|
return (name) ? BLI_findstringindex(&ob->defbase, name, offsetof(bDeformGroup, name)) : -1;
|
2007-08-17 11:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-15 17:28:00 +00:00
|
|
|
/* note, must be freed */
|
2013-03-20 23:14:18 +00:00
|
|
|
int *defgroup_flip_map(Object *ob, int *flip_map_len, const bool use_default)
|
2010-01-15 17:28:00 +00:00
|
|
|
{
|
2014-11-16 13:57:58 +01:00
|
|
|
int defbase_tot = *flip_map_len = BLI_listbase_count(&ob->defbase);
|
2010-01-15 17:28:00 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
if (defbase_tot == 0) {
|
2010-01-15 17:28:00 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2011-11-01 08:11:55 +00:00
|
|
|
bDeformGroup *dg;
|
2013-11-17 04:20:31 +11:00
|
|
|
char name_flip[sizeof(dg->name)];
|
2012-03-20 04:09:43 +00:00
|
|
|
int i, flip_num, *map = MEM_mallocN(defbase_tot * sizeof(int), __func__);
|
2010-01-26 17:07:47 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
for (i = 0; i < defbase_tot; i++) {
|
|
|
|
|
map[i] = -1;
|
2011-11-01 08:11:55 +00:00
|
|
|
}
|
2010-01-15 17:28:00 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
for (dg = ob->defbase.first, i = 0; dg; dg = dg->next, i++) {
|
2011-11-30 07:39:54 +00:00
|
|
|
if (map[i] == -1) { /* may be calculated previously */
|
2010-01-26 17:07:47 +00:00
|
|
|
|
2012-03-09 00:41:09 +00:00
|
|
|
/* in case no valid value is found, use this */
|
2011-11-30 07:39:54 +00:00
|
|
|
if (use_default)
|
2012-03-20 04:09:43 +00:00
|
|
|
map[i] = i;
|
2010-01-26 17:07:47 +00:00
|
|
|
|
2013-11-17 04:20:31 +11:00
|
|
|
BKE_deform_flip_side_name(name_flip, dg->name, false);
|
|
|
|
|
|
|
|
|
|
if (!STREQ(name_flip, dg->name)) {
|
|
|
|
|
flip_num = defgroup_name_index(ob, name_flip);
|
2011-11-30 07:39:54 +00:00
|
|
|
if (flip_num >= 0) {
|
2012-03-20 04:09:43 +00:00
|
|
|
map[i] = flip_num;
|
|
|
|
|
map[flip_num] = i; /* save an extra lookup */
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 08:11:55 +00:00
|
|
|
/* note, must be freed */
|
2013-03-20 23:14:18 +00:00
|
|
|
int *defgroup_flip_map_single(Object *ob, int *flip_map_len, const bool use_default, int defgroup)
|
2011-11-01 08:11:55 +00:00
|
|
|
{
|
2014-11-16 13:57:58 +01:00
|
|
|
int defbase_tot = *flip_map_len = BLI_listbase_count(&ob->defbase);
|
2011-11-01 08:11:55 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
if (defbase_tot == 0) {
|
2011-11-01 08:11:55 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
bDeformGroup *dg;
|
2013-11-17 04:20:31 +11:00
|
|
|
char name_flip[sizeof(dg->name)];
|
2012-03-20 04:09:43 +00:00
|
|
|
int i, flip_num, *map = MEM_mallocN(defbase_tot * sizeof(int), __func__);
|
2011-11-01 08:11:55 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
for (i = 0; i < defbase_tot; i++) {
|
|
|
|
|
map[i] = use_default ? i : -1;
|
2011-11-01 08:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
dg = BLI_findlink(&ob->defbase, defgroup);
|
2011-11-01 08:11:55 +00:00
|
|
|
|
2013-11-17 04:20:31 +11:00
|
|
|
BKE_deform_flip_side_name(name_flip, dg->name, false);
|
|
|
|
|
if (!STREQ(name_flip, dg->name)) {
|
|
|
|
|
flip_num = defgroup_name_index(ob, name_flip);
|
2011-11-01 08:11:55 +00:00
|
|
|
|
2012-10-22 17:19:05 +00:00
|
|
|
if (flip_num != -1) {
|
2012-03-20 04:09:43 +00:00
|
|
|
map[defgroup] = flip_num;
|
|
|
|
|
map[flip_num] = defgroup;
|
2011-11-01 08:11:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-20 23:14:18 +00:00
|
|
|
int defgroup_flip_index(Object *ob, int index, const bool use_default)
|
2010-01-26 17:07:47 +00:00
|
|
|
{
|
2012-03-20 04:09:43 +00:00
|
|
|
bDeformGroup *dg = BLI_findlink(&ob->defbase, index);
|
2010-01-26 17:07:47 +00:00
|
|
|
int flip_index = -1;
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dg) {
|
2013-11-17 04:20:31 +11:00
|
|
|
char name_flip[sizeof(dg->name)];
|
|
|
|
|
BKE_deform_flip_side_name(name_flip, dg->name, false);
|
2010-01-26 17:07:47 +00:00
|
|
|
|
2013-11-17 04:20:31 +11:00
|
|
|
if (!STREQ(name_flip, dg->name)) {
|
|
|
|
|
flip_index = defgroup_name_index(ob, name_flip);
|
|
|
|
|
}
|
2010-01-26 17:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
return (flip_index == -1 && use_default) ? index : flip_index;
|
2010-01-26 17:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-04 19:27:51 +00:00
|
|
|
static bool defgroup_find_name_dupe(const char *name, bDeformGroup *dg, Object *ob)
|
2007-04-27 11:16:35 +00:00
|
|
|
{
|
|
|
|
|
bDeformGroup *curdef;
|
2011-11-30 07:39:54 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
for (curdef = ob->defbase.first; curdef; curdef = curdef->next) {
|
|
|
|
|
if (dg != curdef) {
|
2010-11-01 07:19:41 +00:00
|
|
|
if (!strcmp(curdef->name, name)) {
|
2013-03-04 19:27:51 +00:00
|
|
|
return true;
|
2010-11-01 07:19:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-04 19:27:51 +00:00
|
|
|
return false;
|
2010-11-01 07:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-04 19:27:51 +00:00
|
|
|
static bool defgroup_unique_check(void *arg, const char *name)
|
2010-11-07 08:49:07 +00:00
|
|
|
{
|
2012-05-12 19:18:02 +00:00
|
|
|
struct {Object *ob; void *dg; } *data = arg;
|
2010-11-07 08:49:07 +00:00
|
|
|
return defgroup_find_name_dupe(name, data->dg, data->ob);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
void defgroup_unique_name(bDeformGroup *dg, Object *ob)
|
2010-11-07 08:49:07 +00:00
|
|
|
{
|
2012-05-12 19:18:02 +00:00
|
|
|
struct {Object *ob; void *dg; } data;
|
2012-03-20 04:09:43 +00:00
|
|
|
data.ob = ob;
|
|
|
|
|
data.dg = dg;
|
2008-07-04 19:56:31 +00:00
|
|
|
|
2013-03-25 08:29:06 +00:00
|
|
|
BLI_uniquename_cb(defgroup_unique_check, &data, DATA_("Group"), '.', dg->name, sizeof(dg->name));
|
2010-11-01 07:19:41 +00:00
|
|
|
}
|
2010-01-15 17:28:00 +00:00
|
|
|
|
2014-02-03 18:55:59 +11:00
|
|
|
static bool is_char_sep(const char c)
|
2012-03-20 04:09:43 +00:00
|
|
|
{
|
2014-07-20 01:30:29 +10:00
|
|
|
return ELEM(c, '.', ' ', '-', '_');
|
2012-03-20 04:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-30 08:24:44 +00:00
|
|
|
/* based on BLI_split_dirfile() / os.path.splitext(), "a.b.c" -> ("a.b", ".c") */
|
|
|
|
|
|
|
|
|
|
void BKE_deform_split_suffix(const char string[MAX_VGROUP_NAME], char body[MAX_VGROUP_NAME], char suf[MAX_VGROUP_NAME])
|
|
|
|
|
{
|
|
|
|
|
size_t len = BLI_strnlen(string, MAX_VGROUP_NAME);
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
body[0] = suf[0] = '\0';
|
|
|
|
|
|
2013-07-27 18:16:22 +00:00
|
|
|
for (i = len; i > 0; i--) {
|
2012-04-30 08:24:44 +00:00
|
|
|
if (is_char_sep(string[i])) {
|
|
|
|
|
BLI_strncpy(body, string, i + 1);
|
|
|
|
|
BLI_strncpy(suf, string + i, (len + 1) - i);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-27 18:16:22 +00:00
|
|
|
memcpy(body, string, len + 1);
|
2012-04-30 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* "a.b.c" -> ("a.", "b.c") */
|
|
|
|
|
void BKE_deform_split_prefix(const char string[MAX_VGROUP_NAME], char pre[MAX_VGROUP_NAME], char body[MAX_VGROUP_NAME])
|
|
|
|
|
{
|
|
|
|
|
size_t len = BLI_strnlen(string, MAX_VGROUP_NAME);
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
body[0] = pre[0] = '\0';
|
|
|
|
|
|
|
|
|
|
for (i = 1; i < len; i++) {
|
|
|
|
|
if (is_char_sep(string[i])) {
|
|
|
|
|
i++;
|
|
|
|
|
BLI_strncpy(pre, string, i + 1);
|
|
|
|
|
BLI_strncpy(body, string + i, (len + 1) - i);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_strncpy(body, string, len);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-15 17:28:00 +00:00
|
|
|
/* finds the best possible flipped name. For renaming; check for unique names afterwards */
|
2011-02-13 13:07:12 +00:00
|
|
|
/* if strip_number: removes number extensions
|
2012-03-18 07:38:51 +00:00
|
|
|
* note: don't use sizeof() for 'name' or 'from_name' */
|
2013-11-17 04:20:31 +11:00
|
|
|
void BKE_deform_flip_side_name(char name[MAX_VGROUP_NAME], const char from_name[MAX_VGROUP_NAME],
|
|
|
|
|
const bool strip_number)
|
2010-01-15 17:28:00 +00:00
|
|
|
{
|
|
|
|
|
int len;
|
2012-03-20 04:09:43 +00:00
|
|
|
char prefix[MAX_VGROUP_NAME] = ""; /* The part before the facing */
|
|
|
|
|
char suffix[MAX_VGROUP_NAME] = ""; /* The part after the facing */
|
|
|
|
|
char replace[MAX_VGROUP_NAME] = ""; /* The replacement string */
|
|
|
|
|
char number[MAX_VGROUP_NAME] = ""; /* The number extension string */
|
|
|
|
|
char *index = NULL;
|
2014-08-19 16:16:25 +10:00
|
|
|
bool is_set = false;
|
2010-01-15 17:28:00 +00:00
|
|
|
|
2011-12-09 07:35:56 +00:00
|
|
|
/* always copy the name, since this can be called with an uninitialized string */
|
2011-02-13 13:07:12 +00:00
|
|
|
BLI_strncpy(name, from_name, MAX_VGROUP_NAME);
|
2010-01-17 16:00:54 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
len = BLI_strnlen(from_name, MAX_VGROUP_NAME);
|
2011-12-09 07:35:56 +00:00
|
|
|
if (len < 3) {
|
|
|
|
|
/* we don't do names like .R or .L */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-15 17:28:00 +00:00
|
|
|
/* We first check the case with a .### extension, let's find the last period */
|
2012-03-20 04:09:43 +00:00
|
|
|
if (isdigit(name[len - 1])) {
|
|
|
|
|
index = strrchr(name, '.'); // last occurrence
|
2012-04-29 18:23:33 +00:00
|
|
|
if (index && isdigit(index[1])) { // doesnt handle case bone.1abc2 correct..., whatever!
|
2013-11-17 04:20:31 +11:00
|
|
|
if (strip_number == false) {
|
2011-04-21 15:53:30 +00:00
|
|
|
BLI_strncpy(number, index, sizeof(number));
|
2012-03-20 04:09:43 +00:00
|
|
|
}
|
|
|
|
|
*index = 0;
|
|
|
|
|
len = BLI_strnlen(name, MAX_VGROUP_NAME);
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-13 13:07:12 +00:00
|
|
|
BLI_strncpy(prefix, name, sizeof(prefix));
|
2010-01-15 17:28:00 +00:00
|
|
|
|
|
|
|
|
/* first case; separator . - _ with extensions r R l L */
|
2012-04-30 08:24:44 +00:00
|
|
|
if (is_char_sep(name[len - 2])) {
|
2014-08-19 16:16:25 +10:00
|
|
|
is_set = true;
|
2012-04-28 06:31:57 +00:00
|
|
|
switch (name[len - 1]) {
|
2010-01-15 17:28:00 +00:00
|
|
|
case 'l':
|
2012-03-20 04:09:43 +00:00
|
|
|
prefix[len - 1] = 0;
|
2010-01-15 17:28:00 +00:00
|
|
|
strcpy(replace, "r");
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
2012-03-20 04:09:43 +00:00
|
|
|
prefix[len - 1] = 0;
|
2010-01-15 17:28:00 +00:00
|
|
|
strcpy(replace, "l");
|
|
|
|
|
break;
|
|
|
|
|
case 'L':
|
2012-03-20 04:09:43 +00:00
|
|
|
prefix[len - 1] = 0;
|
2010-01-15 17:28:00 +00:00
|
|
|
strcpy(replace, "R");
|
|
|
|
|
break;
|
|
|
|
|
case 'R':
|
2012-03-20 04:09:43 +00:00
|
|
|
prefix[len - 1] = 0;
|
2010-01-15 17:28:00 +00:00
|
|
|
strcpy(replace, "L");
|
|
|
|
|
break;
|
2014-08-19 16:16:25 +10:00
|
|
|
default:
|
|
|
|
|
is_set = false;
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-19 16:16:25 +10:00
|
|
|
|
2012-05-23 22:45:39 +00:00
|
|
|
/* case; beginning with r R l L, with separator after it */
|
2014-08-19 16:16:25 +10:00
|
|
|
if (!is_set && is_char_sep(name[1])) {
|
|
|
|
|
is_set = true;
|
2012-04-28 06:31:57 +00:00
|
|
|
switch (name[0]) {
|
2010-01-15 17:28:00 +00:00
|
|
|
case 'l':
|
|
|
|
|
strcpy(replace, "r");
|
2012-03-20 04:09:43 +00:00
|
|
|
BLI_strncpy(suffix, name + 1, sizeof(suffix));
|
|
|
|
|
prefix[0] = 0;
|
2010-01-15 17:28:00 +00:00
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
strcpy(replace, "l");
|
2012-03-20 04:09:43 +00:00
|
|
|
BLI_strncpy(suffix, name + 1, sizeof(suffix));
|
|
|
|
|
prefix[0] = 0;
|
2010-01-15 17:28:00 +00:00
|
|
|
break;
|
|
|
|
|
case 'L':
|
|
|
|
|
strcpy(replace, "R");
|
2012-03-20 04:09:43 +00:00
|
|
|
BLI_strncpy(suffix, name + 1, sizeof(suffix));
|
|
|
|
|
prefix[0] = 0;
|
2010-01-15 17:28:00 +00:00
|
|
|
break;
|
|
|
|
|
case 'R':
|
|
|
|
|
strcpy(replace, "L");
|
2012-03-20 04:09:43 +00:00
|
|
|
BLI_strncpy(suffix, name + 1, sizeof(suffix));
|
|
|
|
|
prefix[0] = 0;
|
2010-01-15 17:28:00 +00:00
|
|
|
break;
|
2014-08-19 16:16:25 +10:00
|
|
|
default:
|
|
|
|
|
is_set = false;
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-19 16:16:25 +10:00
|
|
|
|
|
|
|
|
if (!is_set && len > 5) {
|
2010-01-15 17:28:00 +00:00
|
|
|
/* hrms, why test for a separator? lets do the rule 'ultimate left or right' */
|
2014-08-19 16:16:25 +10:00
|
|
|
if (((index = BLI_strcasestr(prefix, "right")) == prefix) ||
|
|
|
|
|
(index == prefix + len - 5))
|
|
|
|
|
{
|
|
|
|
|
is_set = true;
|
|
|
|
|
if (index[0] == 'r') {
|
2012-01-11 12:33:51 +00:00
|
|
|
strcpy(replace, "left");
|
2014-08-19 16:16:25 +10:00
|
|
|
}
|
2010-01-15 17:28:00 +00:00
|
|
|
else {
|
2014-08-19 16:16:25 +10:00
|
|
|
strcpy(replace, (index[1] == 'I') ? "LEFT" : "Left");
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
2012-03-20 04:09:43 +00:00
|
|
|
*index = 0;
|
|
|
|
|
BLI_strncpy(suffix, index + 5, sizeof(suffix));
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
2014-08-19 16:16:25 +10:00
|
|
|
else if (((index = BLI_strcasestr(prefix, "left")) == prefix) ||
|
|
|
|
|
(index == prefix + len - 4))
|
|
|
|
|
{
|
|
|
|
|
is_set = true;
|
|
|
|
|
if (index[0] == 'l') {
|
|
|
|
|
strcpy(replace, "right");
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
2014-08-19 16:16:25 +10:00
|
|
|
else {
|
|
|
|
|
strcpy(replace, (index[1] == 'E') ? "RIGHT" : "Right");
|
|
|
|
|
}
|
|
|
|
|
*index = 0;
|
|
|
|
|
BLI_strncpy(suffix, index + 4, sizeof(suffix));
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 16:16:25 +10:00
|
|
|
(void)is_set; /* quiet warning */
|
|
|
|
|
|
2012-05-12 19:18:02 +00:00
|
|
|
BLI_snprintf(name, MAX_VGROUP_NAME, "%s%s%s%s", prefix, replace, suffix, number);
|
2010-01-15 17:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
float defvert_find_weight(const struct MDeformVert *dvert, const int defgroup)
|
2010-01-26 17:07:47 +00:00
|
|
|
{
|
2012-03-20 04:09:43 +00:00
|
|
|
MDeformWeight *dw = defvert_find_index(dvert, defgroup);
|
2010-01-28 12:16:35 +00:00
|
|
|
return dw ? dw->weight : 0.0f;
|
2010-01-26 17:07:47 +00:00
|
|
|
}
|
2010-01-15 17:28:00 +00:00
|
|
|
|
2011-12-14 22:54:38 +00:00
|
|
|
/* take care with this the rationale is:
|
2012-03-18 07:38:51 +00:00
|
|
|
* - if the object has no vertex group. act like vertex group isn't set and return 1.0,
|
|
|
|
|
* - if the vertex group exists but the 'defgroup' isn't found on this vertex, _still_ return 0.0
|
2011-12-14 22:54:38 +00:00
|
|
|
*
|
|
|
|
|
* This is a bit confusing, just saves some checks from the caller.
|
|
|
|
|
*/
|
2011-11-30 07:39:54 +00:00
|
|
|
float defvert_array_find_weight_safe(const struct MDeformVert *dvert, const int index, const int defgroup)
|
2010-01-26 17:07:47 +00:00
|
|
|
{
|
2011-11-30 07:39:54 +00:00
|
|
|
if (defgroup == -1 || dvert == NULL)
|
2010-01-30 03:47:13 +00:00
|
|
|
return 1.0f;
|
2010-01-15 17:28:00 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
return defvert_find_weight(dvert + index, defgroup);
|
2010-01-26 17:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-12-03 01:52:28 +00:00
|
|
|
MDeformWeight *defvert_find_index(const MDeformVert *dvert, const int defgroup)
|
2008-07-04 19:56:31 +00:00
|
|
|
{
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dvert && defgroup >= 0) {
|
2010-01-26 17:07:47 +00:00
|
|
|
MDeformWeight *dw = dvert->dw;
|
2011-12-14 21:08:08 +00:00
|
|
|
unsigned int i;
|
2008-07-04 19:56:31 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
for (i = dvert->totweight; i != 0; i--, dw++) {
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dw->def_nr == defgroup) {
|
2010-01-26 17:07:47 +00:00
|
|
|
return dw;
|
2011-11-30 07:39:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-07-04 19:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-26 17:07:47 +00:00
|
|
|
return NULL;
|
2008-07-04 19:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-26 17:07:47 +00:00
|
|
|
/* Ensures that mv has a deform weight entry for the specified defweight group */
|
|
|
|
|
/* Note this function is mirrored in editmesh_tools.c, for use for editvertices */
|
2011-11-30 07:39:54 +00:00
|
|
|
MDeformWeight *defvert_verify_index(MDeformVert *dvert, const int defgroup)
|
2008-07-04 19:56:31 +00:00
|
|
|
{
|
2011-11-30 07:39:54 +00:00
|
|
|
MDeformWeight *dw_new;
|
2008-07-04 19:56:31 +00:00
|
|
|
|
2010-01-26 17:07:47 +00:00
|
|
|
/* do this check always, this function is used to check for it */
|
2011-11-30 07:39:54 +00:00
|
|
|
if (!dvert || defgroup < 0)
|
2010-01-26 17:07:47 +00:00
|
|
|
return NULL;
|
2008-07-04 19:56:31 +00:00
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_new = defvert_find_index(dvert, defgroup);
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dw_new)
|
|
|
|
|
return dw_new;
|
2010-01-26 17:07:47 +00:00
|
|
|
|
2013-07-19 10:41:16 +00:00
|
|
|
dw_new = MEM_mallocN(sizeof(MDeformWeight) * (dvert->totweight + 1), "deformWeight");
|
2011-11-30 07:39:54 +00:00
|
|
|
if (dvert->dw) {
|
2012-03-20 04:09:43 +00:00
|
|
|
memcpy(dw_new, dvert->dw, sizeof(MDeformWeight) * dvert->totweight);
|
2011-11-30 07:39:54 +00:00
|
|
|
MEM_freeN(dvert->dw);
|
2010-01-26 17:07:47 +00:00
|
|
|
}
|
2012-03-20 04:09:43 +00:00
|
|
|
dvert->dw = dw_new;
|
2011-11-30 07:39:54 +00:00
|
|
|
dw_new += dvert->totweight;
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_new->weight = 0.0f;
|
|
|
|
|
dw_new->def_nr = defgroup;
|
2010-01-26 17:07:47 +00:00
|
|
|
/* Group index */
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
dvert->totweight++;
|
|
|
|
|
|
|
|
|
|
return dw_new;
|
|
|
|
|
}
|
2010-01-26 17:07:47 +00:00
|
|
|
|
2011-12-08 03:47:45 +00:00
|
|
|
/* TODO. merge with code above! */
|
|
|
|
|
|
|
|
|
|
/* Adds the given vertex to the specified vertex group, with given weight.
|
2012-03-01 12:20:18 +00:00
|
|
|
* warning, this does NOT check for existing, assume caller already knows its not there */
|
2011-12-08 03:47:45 +00:00
|
|
|
void defvert_add_index_notest(MDeformVert *dvert, int defgroup, const float weight)
|
2011-11-30 07:39:54 +00:00
|
|
|
{
|
|
|
|
|
MDeformWeight *dw_new;
|
|
|
|
|
|
2011-12-08 03:47:45 +00:00
|
|
|
/* do this check always, this function is used to check for it */
|
|
|
|
|
if (!dvert || defgroup < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_new = MEM_callocN(sizeof(MDeformWeight) * (dvert->totweight + 1), "defvert_add_to group, new deformWeight");
|
2012-03-20 04:27:14 +00:00
|
|
|
if (dvert->dw) {
|
2012-03-20 04:09:43 +00:00
|
|
|
memcpy(dw_new, dvert->dw, sizeof(MDeformWeight) * dvert->totweight);
|
2011-12-08 03:47:45 +00:00
|
|
|
MEM_freeN(dvert->dw);
|
2011-11-30 07:39:54 +00:00
|
|
|
}
|
2011-12-08 03:47:45 +00:00
|
|
|
dvert->dw = dw_new;
|
|
|
|
|
dw_new += dvert->totweight;
|
|
|
|
|
dw_new->weight = weight;
|
|
|
|
|
dw_new->def_nr = defgroup;
|
|
|
|
|
dvert->totweight++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Removes the given vertex from the vertex group.
|
|
|
|
|
* WARNING: This function frees the given MDeformWeight, do not use it afterward! */
|
|
|
|
|
void defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
|
|
|
|
|
{
|
|
|
|
|
if (dvert && dw) {
|
|
|
|
|
MDeformWeight *dw_new;
|
|
|
|
|
int i = dw - dvert->dw;
|
|
|
|
|
|
2011-11-30 07:39:54 +00:00
|
|
|
/* Security check! */
|
2012-03-20 04:27:14 +00:00
|
|
|
if (i < 0 || i >= dvert->totweight) {
|
2011-11-30 07:39:54 +00:00
|
|
|
return;
|
2011-12-08 03:47:45 +00:00
|
|
|
}
|
2011-11-30 07:39:54 +00:00
|
|
|
|
2011-12-08 03:47:45 +00:00
|
|
|
dvert->totweight--;
|
|
|
|
|
/* If there are still other deform weights attached to this vert then remove
|
|
|
|
|
* this deform weight, and reshuffle the others.
|
|
|
|
|
*/
|
|
|
|
|
if (dvert->totweight) {
|
2012-03-20 04:09:43 +00:00
|
|
|
dw_new = MEM_mallocN(sizeof(MDeformWeight) * (dvert->totweight), __func__);
|
2011-12-14 21:08:08 +00:00
|
|
|
if (dvert->dw) {
|
2012-05-12 19:18:02 +00:00
|
|
|
#if 1 /* since we don't care about order, swap this with the last, save a memcpy */
|
2011-12-14 23:53:46 +00:00
|
|
|
if (i != dvert->totweight) {
|
2012-03-20 04:09:43 +00:00
|
|
|
dvert->dw[i] = dvert->dw[dvert->totweight];
|
2011-12-14 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
memcpy(dw_new, dvert->dw, sizeof(MDeformWeight) * dvert->totweight);
|
|
|
|
|
#else
|
2012-03-20 04:09:43 +00:00
|
|
|
memcpy(dw_new, dvert->dw, sizeof(MDeformWeight) * i);
|
|
|
|
|
memcpy(dw_new + i, dvert->dw + i + 1, sizeof(MDeformWeight) * (dvert->totweight - i));
|
2011-12-14 23:53:46 +00:00
|
|
|
#endif
|
2012-02-05 07:40:15 +00:00
|
|
|
MEM_freeN(dvert->dw);
|
2011-12-08 03:47:45 +00:00
|
|
|
}
|
|
|
|
|
dvert->dw = dw_new;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* If there are no other deform weights left then just remove this one. */
|
2011-11-30 07:39:54 +00:00
|
|
|
MEM_freeN(dvert->dw);
|
2011-12-08 03:47:45 +00:00
|
|
|
dvert->dw = NULL;
|
2011-11-30 07:39:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-26 17:07:47 +00:00
|
|
|
}
|
2012-03-29 13:09:07 +00:00
|
|
|
|
|
|
|
|
void defvert_clear(MDeformVert *dvert)
|
|
|
|
|
{
|
|
|
|
|
if (dvert->dw) {
|
|
|
|
|
MEM_freeN(dvert->dw);
|
|
|
|
|
dvert->dw = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dvert->totweight = 0;
|
|
|
|
|
}
|
2012-10-30 11:40:36 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \return The first group index shared by both deform verts
|
|
|
|
|
* or -1 if none are found.
|
|
|
|
|
*/
|
|
|
|
|
int defvert_find_shared(const MDeformVert *dvert_a, const MDeformVert *dvert_b)
|
|
|
|
|
{
|
|
|
|
|
if (dvert_a->totweight && dvert_b->totweight) {
|
|
|
|
|
MDeformWeight *dw = dvert_a->dw;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
for (i = dvert_a->totweight; i != 0; i--, dw++) {
|
|
|
|
|
if (dw->weight > 0.0f && defvert_find_weight(dvert_b, dw->def_nr) > 0.0f) {
|
|
|
|
|
return dw->def_nr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2012-12-28 09:06:48 +00:00
|
|
|
|
2013-02-22 04:09:04 +00:00
|
|
|
/**
|
|
|
|
|
* return true if has no weights
|
|
|
|
|
*/
|
|
|
|
|
bool defvert_is_weight_zero(const struct MDeformVert *dvert, const int defgroup_tot)
|
|
|
|
|
{
|
|
|
|
|
MDeformWeight *dw = dvert->dw;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
for (i = dvert->totweight; i != 0; i--, dw++) {
|
|
|
|
|
if (dw->weight != 0.0f) {
|
|
|
|
|
/* check the group is in-range, happens on rare situations */
|
|
|
|
|
if (LIKELY(dw->def_nr < defgroup_tot)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-28 09:06:48 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/* Defvert Array functions */
|
|
|
|
|
|
|
|
|
|
void BKE_defvert_array_copy(MDeformVert *dst, const MDeformVert *src, int copycount)
|
|
|
|
|
{
|
|
|
|
|
/* Assumes dst is already set up */
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (!src || !dst)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
memcpy(dst, src, copycount * sizeof(MDeformVert));
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < copycount; i++) {
|
|
|
|
|
if (src[i].dw) {
|
|
|
|
|
dst[i].dw = MEM_mallocN(sizeof(MDeformWeight) * src[i].totweight, "copy_deformWeight");
|
|
|
|
|
memcpy(dst[i].dw, src[i].dw, sizeof(MDeformWeight) * src[i].totweight);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-11 04:43:49 +00:00
|
|
|
void BKE_defvert_array_free_elems(MDeformVert *dvert, int totvert)
|
2012-12-28 09:06:48 +00:00
|
|
|
{
|
|
|
|
|
/* Instead of freeing the verts directly,
|
|
|
|
|
* call this function to delete any special
|
|
|
|
|
* vert data */
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (!dvert)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Free any special data from the verts */
|
|
|
|
|
for (i = 0; i < totvert; i++) {
|
|
|
|
|
if (dvert[i].dw) MEM_freeN(dvert[i].dw);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-11 04:43:49 +00:00
|
|
|
void BKE_defvert_array_free(MDeformVert *dvert, int totvert)
|
|
|
|
|
{
|
|
|
|
|
/* Instead of freeing the verts directly,
|
|
|
|
|
* call this function to delete any special
|
|
|
|
|
* vert data */
|
|
|
|
|
if (!dvert)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Free any special data from the verts */
|
|
|
|
|
BKE_defvert_array_free_elems(dvert, totvert);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(dvert);
|
|
|
|
|
}
|
Transfer Data: add main core code and operators.
This add code needed to map a CD data layout from source mesh towards destination one,
and code needed to actually transfer data, using BKE's mesh remap generated data.
This allows to transfer most CD layers (vgroups, vcols, uvs...) as well as fake, boolean ones
(like smooth/sharp edges/faces, etc.). Some types are not yet transferable, mainly
shape keys, this is known TODO.
Data transfer can also use some advanced mixing in some cases (mostly, vgroups and vcols).
Notes:
* New transfer operators transfer data from active object towards selected ones.
* Modifier will be committed separately.
* Old weight transfer code (for vgroups) is kept for now, mostly because it is the only
usable one in weightpaint mode (it transfers from selected object to active one,
this is not sensible in Object mode, but needed in WeightPaint one). This will be addressed soon.
Again, heavily reviewed and enhanced by Campbell, thanks!
2015-01-09 19:11:40 +01:00
|
|
|
|
|
|
|
|
void BKE_defvert_extract_vgroup_to_vertweights(
|
|
|
|
|
MDeformVert *dvert, const int defgroup, const int num_verts, float *r_weights, const bool invert_vgroup)
|
|
|
|
|
{
|
|
|
|
|
if (dvert && defgroup != -1) {
|
|
|
|
|
int i = num_verts;
|
|
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
const float w = defvert_find_weight(&dvert[i], defgroup);
|
|
|
|
|
r_weights[i] = invert_vgroup ? (1.0f - w) : w;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fill_vn_fl(r_weights, invert_vgroup ? 1.0f : 0.0f, num_verts);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The following three make basic interpolation, using temp vert_weights array to avoid looking up same weight
|
|
|
|
|
* several times. */
|
|
|
|
|
|
|
|
|
|
void BKE_defvert_extract_vgroup_to_edgeweights(
|
|
|
|
|
MDeformVert *dvert, const int defgroup, const int num_verts, MEdge *edges, const int num_edges,
|
|
|
|
|
float *r_weights, const bool invert_vgroup)
|
|
|
|
|
{
|
|
|
|
|
if (dvert && defgroup != -1) {
|
|
|
|
|
int i = num_edges;
|
|
|
|
|
float *tmp_weights = MEM_mallocN(sizeof(*tmp_weights) * (size_t)num_verts, __func__);
|
|
|
|
|
|
|
|
|
|
BKE_defvert_extract_vgroup_to_vertweights(dvert, defgroup, num_verts, tmp_weights, invert_vgroup);
|
|
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
MEdge *me = &edges[i];
|
|
|
|
|
|
|
|
|
|
r_weights[i] = (tmp_weights[me->v1] + tmp_weights[me->v2]) * 0.5f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MEM_freeN(tmp_weights);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fill_vn_fl(r_weights, 0.0f, num_edges);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_defvert_extract_vgroup_to_loopweights(
|
|
|
|
|
MDeformVert *dvert, const int defgroup, const int num_verts, MLoop *loops, const int num_loops,
|
|
|
|
|
float *r_weights, const bool invert_vgroup)
|
|
|
|
|
{
|
|
|
|
|
if (dvert && defgroup != -1) {
|
|
|
|
|
int i = num_loops;
|
|
|
|
|
float *tmp_weights = MEM_mallocN(sizeof(*tmp_weights) * (size_t)num_verts, __func__);
|
|
|
|
|
|
|
|
|
|
BKE_defvert_extract_vgroup_to_vertweights(dvert, defgroup, num_verts, tmp_weights, invert_vgroup);
|
|
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
MLoop *ml = &loops[i];
|
|
|
|
|
|
|
|
|
|
r_weights[i] = tmp_weights[ml->v];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MEM_freeN(tmp_weights);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fill_vn_fl(r_weights, 0.0f, num_loops);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_defvert_extract_vgroup_to_polyweights(
|
|
|
|
|
MDeformVert *dvert, const int defgroup, const int num_verts, MLoop *loops, const int UNUSED(num_loops),
|
|
|
|
|
MPoly *polys, const int num_polys, float *r_weights, const bool invert_vgroup)
|
|
|
|
|
{
|
|
|
|
|
if (dvert && defgroup != -1) {
|
|
|
|
|
int i = num_polys;
|
|
|
|
|
float *tmp_weights = MEM_mallocN(sizeof(*tmp_weights) * (size_t)num_verts, __func__);
|
|
|
|
|
|
|
|
|
|
BKE_defvert_extract_vgroup_to_vertweights(dvert, defgroup, num_verts, tmp_weights, invert_vgroup);
|
|
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
MPoly *mp = &polys[i];
|
|
|
|
|
MLoop *ml = &loops[mp->loopstart];
|
|
|
|
|
int j = mp->totloop;
|
|
|
|
|
float w = 0.0f;
|
|
|
|
|
|
|
|
|
|
for (; j--; ml++) {
|
|
|
|
|
w += tmp_weights[ml->v];
|
|
|
|
|
}
|
|
|
|
|
r_weights[i] = w / (float)mp->totloop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MEM_freeN(tmp_weights);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fill_vn_fl(r_weights, 0.0f, num_polys);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*********** Data Transfer **********/
|
|
|
|
|
|
|
|
|
|
static void vgroups_datatransfer_interp(const CustomDataTransferLayerMap *laymap, void *dest,
|
|
|
|
|
void **sources, const float *weights, const int count, const float mix_factor)
|
|
|
|
|
{
|
|
|
|
|
MDeformVert **data_src = (MDeformVert **)sources;
|
|
|
|
|
MDeformVert *data_dst = (MDeformVert *)dest;
|
|
|
|
|
const int idx_src = laymap->data_src_n;
|
|
|
|
|
const int idx_dst = laymap->data_dst_n;
|
|
|
|
|
|
|
|
|
|
const int mix_mode = laymap->mix_mode;
|
|
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
|
|
MDeformWeight *dw_src;
|
|
|
|
|
MDeformWeight *dw_dst = defvert_find_index(data_dst, idx_dst);
|
|
|
|
|
float weight_src = 0.0f, weight_dst = 0.0f;
|
|
|
|
|
|
|
|
|
|
if (sources) {
|
|
|
|
|
for (i = count; i--;) {
|
|
|
|
|
for (j = data_src[i]->totweight; j--;) {
|
|
|
|
|
if ((dw_src = &data_src[i]->dw[j])->def_nr == idx_src) {
|
|
|
|
|
weight_src += dw_src->weight * weights[i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dw_dst) {
|
|
|
|
|
weight_dst = dw_dst->weight;
|
|
|
|
|
}
|
|
|
|
|
else if (mix_mode == CDT_MIX_REPLACE_ABOVE_THRESHOLD) {
|
|
|
|
|
return; /* Do not affect destination. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
weight_src = data_transfer_interp_float_do(mix_mode, weight_dst, weight_src, mix_factor);
|
|
|
|
|
|
|
|
|
|
CLAMP(weight_src, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
|
|
if (!dw_dst) {
|
|
|
|
|
defvert_add_index_notest(data_dst, idx_dst, weight_src);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
dw_dst->weight = weight_src;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool data_transfer_layersmapping_vgroups_multisrc_to_dst(
|
|
|
|
|
ListBase *r_map, const int mix_mode, const float mix_factor, const float *mix_weights,
|
|
|
|
|
const int num_elem_dst, const bool use_create, const bool use_delete,
|
|
|
|
|
Object *ob_src, Object *ob_dst, MDeformVert *data_src, MDeformVert *data_dst,
|
|
|
|
|
CustomData *UNUSED(cd_src), CustomData *cd_dst, const bool UNUSED(use_dupref_dst),
|
|
|
|
|
const int tolayers, bool *use_layers_src, const int num_layers_src)
|
|
|
|
|
{
|
|
|
|
|
int idx_src;
|
|
|
|
|
int idx_dst;
|
|
|
|
|
int tot_dst = BLI_listbase_count(&ob_dst->defbase);
|
|
|
|
|
|
|
|
|
|
const size_t elem_size = sizeof(*((MDeformVert *)NULL));
|
|
|
|
|
|
|
|
|
|
switch (tolayers) {
|
|
|
|
|
case DT_LAYERS_INDEX_DST:
|
|
|
|
|
idx_dst = tot_dst;
|
|
|
|
|
|
|
|
|
|
/* Find last source actually used! */
|
|
|
|
|
idx_src = num_layers_src;
|
|
|
|
|
while (idx_src-- && !use_layers_src[idx_src]);
|
|
|
|
|
idx_src++;
|
|
|
|
|
|
|
|
|
|
if (idx_dst < idx_src) {
|
|
|
|
|
if (!use_create) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
/* Create as much vgroups as necessary! */
|
|
|
|
|
for (; idx_dst < idx_src; idx_dst++) {
|
|
|
|
|
BKE_object_defgroup_add(ob_dst);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (use_delete && idx_dst > idx_src) {
|
|
|
|
|
while (idx_dst-- > idx_src) {
|
|
|
|
|
BKE_object_defgroup_remove(ob_dst, ob_dst->defbase.last);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (r_map) {
|
|
|
|
|
/* At this stage, we **need** a valid CD_MDEFORMVERT layer on dest!
|
|
|
|
|
* Again, use_create is not relevant in this case */
|
|
|
|
|
if (!data_dst) {
|
|
|
|
|
data_dst = CustomData_add_layer(cd_dst, CD_MDEFORMVERT, CD_CALLOC, NULL, num_elem_dst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (idx_src--) {
|
|
|
|
|
if (!use_layers_src[idx_src]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
data_transfer_layersmapping_add_item(r_map, CD_FAKE_MDEFORMVERT, mix_mode, mix_factor, mix_weights,
|
|
|
|
|
data_src, data_dst, idx_src, idx_src,
|
|
|
|
|
elem_size, 0, 0, 0, vgroups_datatransfer_interp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case DT_LAYERS_NAME_DST:
|
|
|
|
|
{
|
|
|
|
|
bDeformGroup *dg_src, *dg_dst;
|
|
|
|
|
|
|
|
|
|
if (use_delete) {
|
|
|
|
|
/* Remove all unused dst vgroups first, simpler in this case. */
|
|
|
|
|
for (dg_dst = ob_dst->defbase.first; dg_dst;) {
|
|
|
|
|
bDeformGroup *dg_dst_next = dg_dst->next;
|
|
|
|
|
|
|
|
|
|
if (defgroup_name_index(ob_src, dg_dst->name) == -1) {
|
|
|
|
|
BKE_object_defgroup_remove(ob_dst, dg_dst);
|
|
|
|
|
}
|
|
|
|
|
dg_dst = dg_dst_next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (idx_src = 0, dg_src = ob_src->defbase.first;
|
|
|
|
|
idx_src < num_layers_src;
|
|
|
|
|
idx_src++, dg_src = dg_src->next)
|
|
|
|
|
{
|
|
|
|
|
if (!use_layers_src[idx_src]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((idx_dst = defgroup_name_index(ob_dst, dg_src->name)) == -1) {
|
|
|
|
|
if (!use_create) {
|
|
|
|
|
if (r_map) {
|
|
|
|
|
BLI_freelistN(r_map);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
BKE_object_defgroup_add_name(ob_dst, dg_src->name);
|
|
|
|
|
idx_dst = ob_dst->actdef - 1;
|
|
|
|
|
}
|
|
|
|
|
if (r_map) {
|
|
|
|
|
/* At this stage, we **need** a valid CD_MDEFORMVERT layer on dest!
|
|
|
|
|
* use_create is not relevant in this case */
|
|
|
|
|
if (!data_dst) {
|
|
|
|
|
data_dst = CustomData_add_layer(cd_dst, CD_MDEFORMVERT, CD_CALLOC, NULL, num_elem_dst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data_transfer_layersmapping_add_item(
|
|
|
|
|
r_map, CD_FAKE_MDEFORMVERT, mix_mode, mix_factor, mix_weights,
|
|
|
|
|
data_src, data_dst, idx_src, idx_dst,
|
|
|
|
|
elem_size, 0, 0, 0, vgroups_datatransfer_interp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool data_transfer_layersmapping_vgroups(
|
|
|
|
|
ListBase *r_map, const int mix_mode, const float mix_factor, const float *mix_weights,
|
|
|
|
|
const int num_elem_dst, const bool use_create, const bool use_delete, Object *ob_src, Object *ob_dst,
|
|
|
|
|
CustomData *cd_src, CustomData *cd_dst, const bool use_dupref_dst, const int fromlayers, const int tolayers)
|
|
|
|
|
{
|
|
|
|
|
int idx_src, idx_dst;
|
|
|
|
|
MDeformVert *data_src, *data_dst = NULL;
|
|
|
|
|
|
|
|
|
|
const size_t elem_size = sizeof(*((MDeformVert *)NULL));
|
|
|
|
|
|
|
|
|
|
/* Note: VGroups are a bit hairy, since their layout is defined on object level (ob->defbase), while their actual
|
|
|
|
|
* data is a (mesh) CD layer.
|
|
|
|
|
* This implies we may have to handle data layout itself while having NULL data itself,
|
|
|
|
|
* and even have to support NULL data_src in transfer data code (we always create a data_dst, though).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (BLI_listbase_is_empty(&ob_src->defbase)) {
|
|
|
|
|
if (use_delete) {
|
|
|
|
|
BKE_object_defgroup_remove_all(ob_dst);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data_src = CustomData_get_layer(cd_src, CD_MDEFORMVERT);
|
|
|
|
|
|
|
|
|
|
data_dst = CustomData_get_layer(cd_dst, CD_MDEFORMVERT);
|
|
|
|
|
if (data_dst && use_dupref_dst && r_map) {
|
|
|
|
|
/* If dest is a derivedmesh, we do not want to overwrite cdlayers of org mesh! */
|
|
|
|
|
data_dst = CustomData_duplicate_referenced_layer(cd_dst, CD_MDEFORMVERT, num_elem_dst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fromlayers == DT_LAYERS_ACTIVE_SRC || fromlayers >= 0) {
|
|
|
|
|
/* Note: use_delete has not much meaning in this case, ignored. */
|
|
|
|
|
|
|
|
|
|
if (fromlayers >= 0) {
|
|
|
|
|
idx_src = fromlayers;
|
|
|
|
|
BLI_assert(idx_src < BLI_listbase_count(&ob_src->defbase));
|
|
|
|
|
}
|
|
|
|
|
else if ((idx_src = ob_src->actdef - 1) == -1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tolayers >= 0) {
|
|
|
|
|
/* Note: in this case we assume layer exists! */
|
|
|
|
|
idx_dst = tolayers;
|
|
|
|
|
BLI_assert(idx_dst < BLI_listbase_count(&ob_dst->defbase));
|
|
|
|
|
}
|
|
|
|
|
else if (tolayers == DT_LAYERS_ACTIVE_DST) {
|
|
|
|
|
if ((idx_dst = ob_dst->actdef - 1) == -1) {
|
|
|
|
|
bDeformGroup *dg_src;
|
|
|
|
|
if (!use_create) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
dg_src = BLI_findlink(&ob_src->defbase, idx_src);
|
|
|
|
|
BKE_object_defgroup_add_name(ob_dst, dg_src->name);
|
|
|
|
|
idx_dst = ob_dst->actdef - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (tolayers == DT_LAYERS_INDEX_DST) {
|
|
|
|
|
int num = BLI_listbase_count(&ob_src->defbase);
|
|
|
|
|
idx_dst = idx_src;
|
|
|
|
|
if (num <= idx_dst) {
|
|
|
|
|
if (!use_create) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/* Create as much vgroups as necessary! */
|
|
|
|
|
for (; num <= idx_dst; num++) {
|
|
|
|
|
BKE_object_defgroup_add(ob_dst);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (tolayers == DT_LAYERS_NAME_DST) {
|
|
|
|
|
bDeformGroup *dg_src = BLI_findlink(&ob_src->defbase, idx_src);
|
|
|
|
|
if ((idx_dst = defgroup_name_index(ob_dst, dg_src->name)) == -1) {
|
|
|
|
|
if (!use_create) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
BKE_object_defgroup_add_name(ob_dst, dg_src->name);
|
|
|
|
|
idx_dst = ob_dst->actdef - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (r_map) {
|
|
|
|
|
/* At this stage, we **need** a valid CD_MDEFORMVERT layer on dest!
|
|
|
|
|
* use_create is not relevant in this case */
|
|
|
|
|
if (!data_dst) {
|
|
|
|
|
data_dst = CustomData_add_layer(cd_dst, CD_MDEFORMVERT, CD_CALLOC, NULL, num_elem_dst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data_transfer_layersmapping_add_item(r_map, CD_FAKE_MDEFORMVERT, mix_mode, mix_factor, mix_weights,
|
|
|
|
|
data_src, data_dst, idx_src, idx_dst,
|
|
|
|
|
elem_size, 0, 0, 0, vgroups_datatransfer_interp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int num_src, num_sel_unused;
|
|
|
|
|
bool *use_layers_src = NULL;
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
|
|
switch (fromlayers) {
|
|
|
|
|
case DT_LAYERS_ALL_SRC:
|
|
|
|
|
use_layers_src = BKE_object_defgroup_subset_from_select_type(ob_src, WT_VGROUP_ALL,
|
|
|
|
|
&num_src, &num_sel_unused);
|
|
|
|
|
break;
|
|
|
|
|
case DT_LAYERS_VGROUP_SRC_BONE_SELECT:
|
|
|
|
|
use_layers_src = BKE_object_defgroup_subset_from_select_type(ob_src, WT_VGROUP_BONE_SELECT,
|
|
|
|
|
&num_src, &num_sel_unused);
|
|
|
|
|
break;
|
|
|
|
|
case DT_LAYERS_VGROUP_SRC_BONE_DEFORM:
|
|
|
|
|
use_layers_src = BKE_object_defgroup_subset_from_select_type(ob_src, WT_VGROUP_BONE_DEFORM,
|
|
|
|
|
&num_src, &num_sel_unused);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (use_layers_src) {
|
|
|
|
|
ret = data_transfer_layersmapping_vgroups_multisrc_to_dst(
|
|
|
|
|
r_map, mix_mode, mix_factor, mix_weights, num_elem_dst, use_create, use_delete,
|
|
|
|
|
ob_src, ob_dst, data_src, data_dst, cd_src, cd_dst, use_dupref_dst,
|
|
|
|
|
tolayers, use_layers_src, num_src);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MEM_SAFE_FREE(use_layers_src);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|