04e05f1ee5e36974bda324904934d85593cc64b0
[blender.git] / source / blender / editors / object / object_vgroup.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): Ove M Henriksen.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/object/object_vgroup.c
29  *  \ingroup edobj
30  */
31
32 #include <string.h>
33 #include <stddef.h>
34 #include <math.h>
35 #include <assert.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_cloth_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_lattice_types.h"
42 #include "DNA_meshdata_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_object_force.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_particle_types.h"
49
50 #include "BLI_alloca.h"
51 #include "BLI_array.h"
52 #include "BLI_math.h"
53 #include "BLI_blenlib.h"
54 #include "BLI_utildefines.h"
55 #include "BLI_linklist_stack.h"
56
57 #include "BLF_translation.h"
58
59 #include "BKE_context.h"
60 #include "BKE_customdata.h"
61 #include "BKE_deform.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_mesh_mapping.h"
64 #include "BKE_editmesh.h"
65 #include "BKE_modifier.h"
66 #include "BKE_report.h"
67 #include "BKE_DerivedMesh.h"
68 #include "BKE_object_deform.h"
69 #include "BKE_object.h"
70 #include "BKE_lattice.h"
71
72 #include "DNA_armature_types.h"
73 #include "RNA_access.h"
74 #include "RNA_define.h"
75 #include "RNA_enum_types.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "ED_object.h"
81 #include "ED_mesh.h"
82
83 #include "UI_resources.h"
84
85 #include "object_intern.h"
86
87 /************************ Exported Functions **********************/
88 static void vgroup_remap_update_users(Object *ob, int *map);
89 static void vgroup_delete_edit_mode(Object *ob, bDeformGroup *defgroup);
90 static void vgroup_delete_object_mode(Object *ob, bDeformGroup *dg);
91 static void vgroup_delete_all(Object *ob);
92
93 static bool vertex_group_use_vert_sel(Object *ob)
94 {
95         if (ob->mode == OB_MODE_EDIT) {
96                 return true;
97         }
98         else if (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_VERT_SEL) {
99                 return true;
100         }
101         else {
102                 return false;
103         }
104 }
105
106 static Lattice *vgroup_edit_lattice(Object *ob)
107 {
108         Lattice *lt = ob->data;
109         BLI_assert(ob->type == OB_LATTICE);
110         return (lt->editlatt) ? lt->editlatt->latt : lt;
111 }
112
113 bool ED_vgroup_sync_from_pose(Object *ob)
114 {
115         Object *armobj = BKE_object_pose_armature_get(ob);
116         if (armobj && (armobj->mode & OB_MODE_POSE)) {
117                 struct bArmature *arm = armobj->data;
118                 if (arm->act_bone) {
119                         int def_num = defgroup_name_index(ob, arm->act_bone->name);
120                         if (def_num != -1) {
121                                 ob->actdef = def_num + 1;
122                                 return true;
123                         }
124                 }
125         }
126         return false;
127 }
128
129 bool ED_vgroup_object_is_edit_mode(Object *ob)
130 {
131         if (ob->type == OB_MESH)
132                 return (BKE_editmesh_from_object(ob) != NULL);
133         else if (ob->type == OB_LATTICE)
134                 return (((Lattice *)ob->data)->editlatt != NULL);
135
136         return false;
137 }
138
139 bDeformGroup *ED_vgroup_add_name(Object *ob, const char *name)
140 {
141         bDeformGroup *defgroup;
142
143         if (!ob || !OB_TYPE_SUPPORT_VGROUP(ob->type))
144                 return NULL;
145
146         defgroup = BKE_defgroup_new(ob, name);
147
148         ob->actdef = BLI_countlist(&ob->defbase);
149
150         return defgroup;
151 }
152
153 bDeformGroup *ED_vgroup_add(Object *ob) 
154 {
155         return ED_vgroup_add_name(ob, DATA_("Group"));
156 }
157
158 void ED_vgroup_delete(Object *ob, bDeformGroup *defgroup) 
159 {
160         BLI_assert(BLI_findindex(&ob->defbase, defgroup) != -1);
161
162         if (ED_vgroup_object_is_edit_mode(ob))
163                 vgroup_delete_edit_mode(ob, defgroup);
164         else
165                 vgroup_delete_object_mode(ob, defgroup);
166 }
167
168 void ED_vgroup_clear(Object *ob)
169 {
170         bDeformGroup *dg = (bDeformGroup *)ob->defbase.first;
171         int edit_mode = ED_vgroup_object_is_edit_mode(ob);
172
173         while (dg) {
174                 bDeformGroup *next_dg = dg->next;
175
176                 if (edit_mode)
177                         vgroup_delete_edit_mode(ob, dg);
178                 else
179                         vgroup_delete_object_mode(ob, dg);
180
181                 dg = next_dg;
182         }
183 }
184
185 bool ED_vgroup_data_create(ID *id)
186 {
187         /* create deform verts */
188
189         if (GS(id->name) == ID_ME) {
190                 Mesh *me = (Mesh *)id;
191                 me->dvert = CustomData_add_layer(&me->vdata, CD_MDEFORMVERT, CD_CALLOC, NULL, me->totvert);
192                 return true;
193         }
194         else if (GS(id->name) == ID_LT) {
195                 Lattice *lt = (Lattice *)id;
196                 lt->dvert = MEM_callocN(sizeof(MDeformVert) * lt->pntsu * lt->pntsv * lt->pntsw, "lattice deformVert");
197                 return true;
198         }
199         else {
200                 return false;
201         }
202 }
203
204 /**
205  * Removes out of range MDeformWeights
206  */
207 void ED_vgroup_data_clamp_range(ID *id, const int total)
208 {
209         MDeformVert **dvert_arr;
210         int dvert_tot;
211
212         if (ED_vgroup_parray_alloc(id, &dvert_arr, &dvert_tot, false)) {
213                 int i;
214                 for (i = 0; i < dvert_tot; i++) {
215                         MDeformVert *dv = dvert_arr[i];
216                         int j;
217                         for (j = 0; j < dv->totweight; j++) {
218                                 if (dv->dw[j].def_nr >= total) {
219                                         defvert_remove_group(dv, &dv->dw[j]);
220                                         j--;
221                                 }
222                         }
223                 }
224         }
225 }
226
227 bool ED_vgroup_parray_alloc(ID *id, MDeformVert ***dvert_arr, int *dvert_tot, const bool use_vert_sel)
228 {
229         *dvert_tot = 0;
230         *dvert_arr = NULL;
231
232         if (id) {
233                 switch (GS(id->name)) {
234                         case ID_ME:
235                         {
236                                 Mesh *me = (Mesh *)id;
237
238                                 if (me->edit_btmesh) {
239                                         BMEditMesh *em = me->edit_btmesh;
240                                         BMesh *bm = em->bm;
241                                         const int cd_dvert_offset  = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
242                                         BMIter iter;
243                                         BMVert *eve;
244                                         int i;
245
246                                         if (cd_dvert_offset == -1) {
247                                                 return false;
248                                         }
249
250                                         i = em->bm->totvert;
251
252                                         *dvert_arr = MEM_mallocN(sizeof(void *) * i, "vgroup parray from me");
253                                         *dvert_tot = i;
254
255                                         i = 0;
256                                         if (use_vert_sel) {
257                                                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
258                                                         (*dvert_arr)[i] = BM_elem_flag_test(eve, BM_ELEM_SELECT) ?
259                                                                           BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset) : NULL;
260                                                         i++;
261                                                 }
262                                         }
263                                         else {
264                                                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
265                                                         (*dvert_arr)[i] = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
266                                                         i++;
267                                                 }
268                                         }
269
270                                         return true;
271                                 }
272                                 else if (me->dvert) {
273                                         MVert *mvert = me->mvert;
274                                         MDeformVert *dvert = me->dvert;
275                                         int i;
276
277                                         *dvert_tot = me->totvert;
278                                         *dvert_arr = MEM_mallocN(sizeof(void *) * me->totvert, "vgroup parray from me");
279
280                                         if (use_vert_sel) {
281                                                 for (i = 0; i < me->totvert; i++) {
282                                                         (*dvert_arr)[i] = (mvert[i].flag & SELECT) ?
283                                                                           &dvert[i] : NULL;
284                                                 }
285                                         }
286                                         else {
287                                                 for (i = 0; i < me->totvert; i++) {
288                                                         (*dvert_arr)[i] = me->dvert + i;
289                                                 }
290                                         }
291
292                                         return true;
293                                 }
294                                 return false;
295                         }
296                         case ID_LT:
297                         {
298                                 int i = 0;
299
300                                 Lattice *lt = (Lattice *)id;
301                                 lt = (lt->editlatt) ? lt->editlatt->latt : lt;
302
303                                 if (lt->dvert) {
304                                         BPoint *def = lt->def;
305                                         *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
306                                         *dvert_arr = MEM_mallocN(sizeof(void *) * (*dvert_tot), "vgroup parray from me");
307
308                                         if (use_vert_sel) {
309                                                 for (i = 0; i < *dvert_tot; i++) {
310                                                         (*dvert_arr)[i] = (def->f1 & SELECT) ?
311                                                                            &lt->dvert[i] : NULL;
312                                                 }
313                                         }
314                                         else {
315                                                 for (i = 0; i < *dvert_tot; i++) {
316                                                         (*dvert_arr)[i] = lt->dvert + i;
317                                                 }
318                                         }
319
320                                         return true;
321                                 }
322                                 return false;
323                         }
324                 }
325         }
326
327         return false;
328 }
329
330 static bool ed_vgroup_dm_parray_alloc(DerivedMesh *dm, MDeformVert ***dvert_arr, int *dvert_tot)
331 {
332         *dvert_tot = 0;
333         *dvert_arr = NULL;
334
335         if (dm) {
336                 MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
337
338                 if (dvert) {
339                         int i, totvert = dm->getNumVerts(dm);
340
341                         *dvert_tot = totvert;
342                         *dvert_arr = MEM_mallocN(sizeof(void *) * totvert, "vgroup parray from me");
343
344                         for (i = 0; i < totvert; i++) {
345                                 (*dvert_arr)[i] = dvert + i;
346                         }
347
348                         return true;
349                 }
350         }
351
352         return false;
353 }
354
355 /**
356  * For use with tools that use ED_vgroup_parray_alloc with \a use_vert_sel == true.
357  * This finds the unselected mirror deform verts and copys the weights to them from the selected.
358  *
359  * \note \a dvert_array has mirrored weights filled in, incase cleanup operations are needed on both.
360  */
361 void ED_vgroup_parray_mirror_sync(Object *ob,
362                                   MDeformVert **dvert_array, const int dvert_tot,
363                                   const bool *vgroup_validmap, const int vgroup_tot)
364 {
365         BMEditMesh *em = BKE_editmesh_from_object(ob);
366         MDeformVert **dvert_array_all = NULL;
367         int dvert_tot_all;
368         int i;
369
370         /* get an array of all verts, not only selected */
371         if (ED_vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
372                 BLI_assert(0);
373                 return;
374         }
375         if (em) {
376                 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
377         }
378
379         for (i = 0; i < dvert_tot; i++) {
380                 if (dvert_array[i] == NULL) {
381                         /* its unselected, check if its mirror is */
382                         int i_sel = ED_mesh_mirror_get_vert(ob, i);
383                         if ((i_sel != -1) && (i_sel != i) && (dvert_array[i_sel])) {
384                                 /* we found a match! */
385                                 MDeformVert *dv_src = dvert_array[i_sel];
386                                 MDeformVert *dv_dst = dvert_array_all[i];
387
388                                 defvert_copy_subset(dv_dst, dv_src, vgroup_validmap, vgroup_tot);
389
390                                 dvert_array[i] = dvert_array_all[i];
391                         }
392                 }
393         }
394
395         MEM_freeN(dvert_array_all);
396 }
397
398 /**
399  * Fill in the pointers for mirror verts (as if all mirror verts were selected too).
400  *
401  * similar to #ED_vgroup_parray_mirror_sync but only fill in mirror points.
402  */
403 void ED_vgroup_parray_mirror_assign(Object *ob,
404                                     MDeformVert **dvert_array, const int dvert_tot)
405 {
406         BMEditMesh *em = BKE_editmesh_from_object(ob);
407         MDeformVert **dvert_array_all = NULL;
408         int dvert_tot_all;
409         int i;
410
411         /* get an array of all verts, not only selected */
412         if (ED_vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
413                 BLI_assert(0);
414                 return;
415         }
416         BLI_assert(dvert_tot == dvert_tot_all);
417         if (em) {
418                 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
419         }
420
421         for (i = 0; i < dvert_tot; i++) {
422                 if (dvert_array[i] == NULL) {
423                         /* its unselected, check if its mirror is */
424                         int i_sel = ED_mesh_mirror_get_vert(ob, i);
425                         if ((i_sel != -1) && (i_sel != i) && (dvert_array[i_sel])) {
426                                 /* we found a match! */
427                                 dvert_array[i] = dvert_array_all[i];
428                         }
429                 }
430         }
431
432         MEM_freeN(dvert_array_all);
433 }
434
435 void ED_vgroup_parray_remove_zero(MDeformVert **dvert_array, const int dvert_tot,
436                                   const bool *vgroup_validmap, const int vgroup_tot,
437                                   const float epsilon, const bool keep_single)
438 {
439         MDeformVert *dv;
440         int i;
441
442         for (i = 0; i < dvert_tot; i++) {
443                 int j;
444
445                 /* in case its not selected */
446                 if (!(dv = dvert_array[i])) {
447                         continue;
448                 }
449
450                 j = dv->totweight;
451
452                 while (j--) {
453                         MDeformWeight *dw;
454
455                         if (keep_single && dv->totweight == 1)
456                                 break;
457
458                         dw = dv->dw + j;
459                         if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
460                                 if (dw->weight <= epsilon) {
461                                         defvert_remove_group(dv, dw);
462                                 }
463                         }
464                 }
465         }
466 }
467
468 /* returns true if the id type supports weights */
469 bool ED_vgroup_array_get(ID *id, MDeformVert **dvert_arr, int *dvert_tot)
470 {
471         if (id) {
472                 switch (GS(id->name)) {
473                         case ID_ME:
474                         {
475                                 Mesh *me = (Mesh *)id;
476                                 *dvert_arr = me->dvert;
477                                 *dvert_tot = me->totvert;
478                                 return true;
479                         }
480                         case ID_LT:
481                         {
482                                 Lattice *lt = (Lattice *)id;
483                                 lt = (lt->editlatt) ? lt->editlatt->latt : lt;
484                                 *dvert_arr = lt->dvert;
485                                 *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
486                                 return true;
487                         }
488                 }
489         }
490
491         *dvert_arr = NULL;
492         *dvert_tot = 0;
493         return false;
494 }
495
496 /* matching index only */
497 bool ED_vgroup_array_copy(Object *ob, Object *ob_from)
498 {
499         MDeformVert **dvert_array_from, **dvf;
500         MDeformVert **dvert_array, **dv;
501         int dvert_tot_from;
502         int dvert_tot;
503         int i;
504         int defbase_tot_from = BLI_countlist(&ob_from->defbase);
505         int defbase_tot = BLI_countlist(&ob->defbase);
506         bool new_vgroup = false;
507
508         if (ob == ob_from)
509                 return true;
510
511         ED_vgroup_parray_alloc(ob_from->data, &dvert_array_from, &dvert_tot_from, false);
512         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
513
514         if ((dvert_array == NULL) && (dvert_array_from != NULL) && ED_vgroup_data_create(ob->data)) {
515                 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
516                 new_vgroup = true;
517         }
518
519         if (dvert_tot == 0 || (dvert_tot != dvert_tot_from) || dvert_array_from == NULL || dvert_array == NULL) {
520
521                 if (dvert_array) MEM_freeN(dvert_array);
522                 if (dvert_array_from) MEM_freeN(dvert_array_from);
523
524                 if (new_vgroup == true) {
525                         /* free the newly added vgroup since it wasn't compatible */
526                         vgroup_delete_all(ob);
527                 }
528
529                 /* if true: both are 0 and nothing needs changing, consider this a success */
530                 return (dvert_tot == dvert_tot_from);
531         }
532
533         /* do the copy */
534         BLI_freelistN(&ob->defbase);
535         BLI_duplicatelist(&ob->defbase, &ob_from->defbase);
536         ob->actdef = ob_from->actdef;
537
538         if (defbase_tot_from < defbase_tot) {
539                 /* correct vgroup indices because the number of vgroups is being reduced. */
540                 int *remap = MEM_mallocN(sizeof(int) * (defbase_tot + 1), __func__);
541                 for (i = 0; i <= defbase_tot_from; i++) remap[i] = i;
542                 for (; i <= defbase_tot; i++) remap[i] = 0;  /* can't use these, so disable */
543
544                 vgroup_remap_update_users(ob, remap);
545                 MEM_freeN(remap);
546         }
547
548         dvf = dvert_array_from;
549         dv = dvert_array;
550
551         for (i = 0; i < dvert_tot; i++, dvf++, dv++) {
552                 if ((*dv)->dw)
553                         MEM_freeN((*dv)->dw);
554
555                 *(*dv) = *(*dvf);
556
557                 if ((*dv)->dw)
558                         (*dv)->dw = MEM_dupallocN((*dv)->dw);
559         }
560
561         MEM_freeN(dvert_array);
562         MEM_freeN(dvert_array_from);
563
564         return true;
565 }
566
567 /* TODO, cache flip data to speedup calls within a loop. */
568 static void mesh_defvert_mirror_update_internal(Object *ob,
569                                                 MDeformVert *dvert_dst, MDeformVert *dvert_src,
570                                                 const int def_nr)
571 {
572         if (def_nr == -1) {
573                 /* all vgroups, add groups where neded  */
574                 int flip_map_len;
575                 int *flip_map = defgroup_flip_map(ob, &flip_map_len, true);
576                 defvert_sync_mapped(dvert_dst, dvert_src, flip_map, flip_map_len, true);
577                 MEM_freeN(flip_map);
578         }
579         else {
580                 /* single vgroup */
581                 MDeformWeight *dw = defvert_verify_index(dvert_dst, defgroup_flip_index(ob, def_nr, 1));
582                 if (dw) {
583                         dw->weight = defvert_find_weight(dvert_src, def_nr);
584                 }
585         }
586 }
587
588 static void ED_mesh_defvert_mirror_update_em(Object *ob, BMVert *eve, int def_nr, int vidx,
589                                              const int cd_dvert_offset)
590 {
591         Mesh *me = ob->data;
592         BMEditMesh *em = me->edit_btmesh;
593         BMVert *eve_mirr;
594         bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
595
596         eve_mirr = editbmesh_get_x_mirror_vert(ob, em, eve, eve->co, vidx, use_topology);
597
598         if (eve_mirr && eve_mirr != eve) {
599                 MDeformVert *dvert_src = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
600                 MDeformVert *dvert_dst = BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset);
601                 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
602         }
603 }
604
605 static void ED_mesh_defvert_mirror_update_ob(Object *ob, int def_nr, int vidx)
606 {
607         int vidx_mirr;
608         Mesh *me = ob->data;
609         bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
610
611         if (vidx == -1)
612                 return;
613
614         vidx_mirr = mesh_get_x_mirror_vert(ob, vidx, use_topology);
615
616         if ((vidx_mirr) >= 0 && (vidx_mirr != vidx)) {
617                 MDeformVert *dvert_src = &me->dvert[vidx];
618                 MDeformVert *dvert_dst = &me->dvert[vidx_mirr];
619                 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
620         }
621 }
622
623 /**
624  * Use when adjusting the active vertex weight and apply to mirror vertices.
625  */
626 void ED_vgroup_vert_active_mirror(Object *ob, int def_nr)
627 {
628         Mesh *me = ob->data;
629         BMEditMesh *em = me->edit_btmesh;
630         MDeformVert *dvert_act;
631
632         if (me->editflag & ME_EDIT_MIRROR_X) {
633                 if (em) {
634                         BMVert *eve_act;
635                         dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
636                         if (dvert_act) {
637                                 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
638                                 ED_mesh_defvert_mirror_update_em(ob, eve_act, def_nr, -1, cd_dvert_offset);
639                         }
640                 }
641                 else {
642                         int v_act;
643                         dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
644                         if (dvert_act) {
645                                 ED_mesh_defvert_mirror_update_ob(ob, def_nr, v_act);
646                         }
647                 }
648         }
649 }
650
651 static void vgroup_remove_weight(Object *ob, const int def_nr)
652 {
653         MDeformVert *dvert_act;
654         MDeformWeight *dw;
655
656         dvert_act = ED_mesh_active_dvert_get_only(ob);
657
658         dw = defvert_find_index(dvert_act, def_nr);
659         defvert_remove_group(dvert_act, dw);
660
661 }
662
663 static void vgroup_normalize_active(Object *ob, eVGroupSelect subset_type)
664 {
665         Mesh *me = ob->data;
666         BMEditMesh *em = me->edit_btmesh;
667         BMVert *eve_act;
668         int v_act;
669         MDeformVert *dvert_act;
670         int subset_count, vgroup_tot;
671         const bool *vgroup_validmap;
672
673
674         if (em) {
675                 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
676         }
677         else {
678                 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
679         }
680
681         if (dvert_act == NULL) {
682                 return;
683         }
684
685         vgroup_validmap = ED_vgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
686         defvert_normalize_subset(dvert_act, vgroup_validmap, vgroup_tot);
687         MEM_freeN((void *)vgroup_validmap);
688
689         if (me->editflag & ME_EDIT_MIRROR_X) {
690                 if (em) {
691                         const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
692                         ED_mesh_defvert_mirror_update_em(ob, eve_act, -1, -1, cd_dvert_offset);
693                 }
694                 else {
695                         ED_mesh_defvert_mirror_update_ob(ob, -1, v_act);
696                 }
697         }
698 }
699
700 static void vgroup_copy_active_to_sel(Object *ob, eVGroupSelect subset_type)
701 {
702         Mesh *me = ob->data;
703         BMEditMesh *em = me->edit_btmesh;
704         MDeformVert *dvert_act;
705         int i, vgroup_tot, subset_count;
706         const bool *vgroup_validmap = ED_vgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
707
708
709         if (em) {
710                 BMIter iter;
711                 BMVert *eve, *eve_act;
712                 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
713
714                 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
715                 if (dvert_act) {
716                         BM_ITER_MESH_INDEX (eve, &iter, em->bm, BM_VERTS_OF_MESH, i) {
717                                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && eve != eve_act) {
718                                         MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
719                                         defvert_copy_subset(dv, dvert_act, vgroup_validmap, vgroup_tot);
720                                         if (me->editflag & ME_EDIT_MIRROR_X) {
721                                                 ED_mesh_defvert_mirror_update_em(ob, eve, -1, i, cd_dvert_offset);
722                                         }
723                                 }
724                         }
725                 }
726         }
727         else {
728                 MDeformVert *dv;
729                 int v_act;
730
731                 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
732                 if (dvert_act) {
733                         dv  = me->dvert;
734                         for (i = 0; i < me->totvert; i++, dv++) {
735                                 if ((me->mvert[i].flag & SELECT) && dv != dvert_act) {
736                                         defvert_copy_subset(dv, dvert_act, vgroup_validmap, vgroup_tot);
737                                         if (me->editflag & ME_EDIT_MIRROR_X) {
738                                                 ED_mesh_defvert_mirror_update_ob(ob, -1, i);
739                                         }
740                                 }
741                         }
742                 }
743         }
744
745         MEM_freeN((void *)vgroup_validmap);
746 }
747
748 /***********************Start weight transfer (WT)*********************************/
749
750 typedef enum WT_VertexGroupMode {
751         WT_REPLACE_ACTIVE_VERTEX_GROUP = 1,
752         WT_REPLACE_ALL_VERTEX_GROUPS = 2
753 } WT_VertexGroupMode;
754
755 typedef enum WT_Method {
756         WT_BY_INDEX = 1,
757         WT_BY_NEAREST_VERTEX = 2,
758         WT_BY_NEAREST_FACE = 3,
759         WT_BY_NEAREST_VERTEX_IN_FACE = 4
760 } WT_Method;
761
762 typedef enum WT_ReplaceMode {
763         WT_REPLACE_ALL_WEIGHTS = 1,
764         WT_REPLACE_EMPTY_WEIGHTS = 2
765 } WT_ReplaceMode;
766
767 static EnumPropertyItem WT_vertex_group_mode_item[] = {
768         {WT_REPLACE_ACTIVE_VERTEX_GROUP,
769          "WT_REPLACE_ACTIVE_VERTEX_GROUP", 0, "Active", "Transfer active vertex group from selected to active mesh"},
770         {WT_REPLACE_ALL_VERTEX_GROUPS,
771          "WT_REPLACE_ALL_VERTEX_GROUPS", 0, "All", "Transfer all vertex groups from selected to active mesh"},
772         {0, NULL, 0, NULL, NULL}
773 };
774
775 static EnumPropertyItem WT_method_item[] = {
776         {WT_BY_INDEX,
777          "WT_BY_INDEX", 0, "Vertex index", "Copy for identical meshes"},
778         {WT_BY_NEAREST_VERTEX,
779          "WT_BY_NEAREST_VERTEX", 0, "Nearest vertex", "Copy weight from closest vertex"},
780         {WT_BY_NEAREST_FACE,
781          "WT_BY_NEAREST_FACE", 0, "Nearest face", "Barycentric interpolation from nearest face"},
782         {WT_BY_NEAREST_VERTEX_IN_FACE,
783          "WT_BY_NEAREST_VERTEX_IN_FACE", 0, "Nearest vertex in face", "Copy weight from closest vertex in nearest face"},
784         {0, NULL, 0, NULL, NULL}
785 };
786
787 static EnumPropertyItem WT_replace_mode_item[] = {
788         {WT_REPLACE_ALL_WEIGHTS,
789          "WT_REPLACE_ALL_WEIGHTS", 0, "All", "Overwrite all weights"},
790         {WT_REPLACE_EMPTY_WEIGHTS,
791          "WT_REPLACE_EMPTY_WEIGHTS", 0, "Empty", "Add weights to vertices with no weight"},
792         {0, NULL, 0, NULL, NULL}
793 };
794
795 static EnumPropertyItem WT_vertex_group_select_item[] = {
796         {WT_VGROUP_ACTIVE,
797          "ACTIVE", 0, "Active Group", "The active Vertex Group"},
798         {WT_VGROUP_BONE_SELECT,
799          "BONE_SELECT", 0, "Selected Pose Bones", "All Vertex Groups assigned to Selection"},
800         {WT_VGROUP_BONE_DEFORM,
801          "BONE_DEFORM", 0, "Deform Pose Bones", "All Vertex Groups assigned to Deform Bones"},
802         {WT_VGROUP_ALL,
803          "ALL", 0, "All Groups", "All Vertex Groups"},
804         {0, NULL, 0, NULL, NULL}
805 };
806
807 EnumPropertyItem *ED_object_vgroup_selection_itemf_helper(
808         const bContext *C, PointerRNA *UNUSED(ptr),
809         PropertyRNA *UNUSED(prop), bool *r_free, const unsigned int selection_mask)
810 {
811         Object *ob;
812         EnumPropertyItem *item = NULL;
813         int totitem = 0;
814
815
816         if (!C) /* needed for docs and i18n tools */
817                 return WT_vertex_group_select_item;
818
819         ob = CTX_data_active_object(C);
820         if (selection_mask & (1 << WT_VGROUP_ACTIVE))
821                 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_ACTIVE);
822
823         if (BKE_object_pose_armature_get(ob)) {
824                 if (selection_mask & (1 << WT_VGROUP_BONE_SELECT))
825                         RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_BONE_SELECT);
826                 if (selection_mask & (1 << WT_VGROUP_BONE_DEFORM))
827                         RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_BONE_DEFORM);
828         }
829
830         if (selection_mask & (1 << WT_VGROUP_ALL))
831                 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_ALL);
832
833         RNA_enum_item_end(&item, &totitem);
834         *r_free = true;
835
836         return item;
837 }
838
839 static EnumPropertyItem *rna_vertex_group_with_single_itemf(bContext *C, PointerRNA *ptr,
840                                                             PropertyRNA *prop, bool *r_free)
841 {
842         return ED_object_vgroup_selection_itemf_helper(C, ptr, prop, r_free, WT_VGROUP_MASK_ALL);
843 }
844
845 static EnumPropertyItem *rna_vertex_group_select_itemf(bContext *C, PointerRNA *ptr,
846                                                        PropertyRNA *prop, bool *r_free)
847 {
848         return ED_object_vgroup_selection_itemf_helper(C, ptr, prop, r_free, WT_VGROUP_MASK_ALL & ~(1 << WT_VGROUP_ACTIVE));
849 }
850
851 static void vgroup_operator_subset_select_props(wmOperatorType *ot, bool use_active)
852 {
853         PropertyRNA *prop;
854
855         prop = RNA_def_enum(ot->srna,
856                             "group_select_mode", DummyRNA_NULL_items,
857                             use_active ? WT_VGROUP_ACTIVE : WT_VGROUP_ALL, "Subset",
858                             "Define which subset of Groups shall be used");
859
860         if (use_active) {
861                 RNA_def_enum_funcs(prop, rna_vertex_group_with_single_itemf);
862         }
863         else {
864                 RNA_def_enum_funcs(prop, rna_vertex_group_select_itemf);
865         }
866         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
867         ot->prop = prop;
868 }
869
870 /* Copy weight.*/
871 static void vgroup_transfer_weight(float *r_weight_dst, const float weight_src, const WT_ReplaceMode replace_mode)
872 {
873         switch (replace_mode) {
874                 case WT_REPLACE_ALL_WEIGHTS:
875                         *r_weight_dst = weight_src;
876                         break;
877
878                 case WT_REPLACE_EMPTY_WEIGHTS:
879                         if (*r_weight_dst == 0.0f) {
880                                 *r_weight_dst = weight_src;
881                         }
882                         break;
883
884                 default:
885                         BLI_assert(0);
886                         break;
887         }
888 }
889
890 /* Could be exposed externally by implementing it in header with the rest.
891  * Simple refactoring will break something.
892  * For now, naming is ed_ instead of ED_*/
893 static bool ed_vgroup_transfer_weight(Object *ob_dst, Object *ob_src, bDeformGroup *dg_src, Scene *scene,
894                                       WT_Method method, WT_ReplaceMode replace_mode, wmOperator *op)
895 {
896         bDeformGroup *dg_dst;
897         Mesh *me_dst;
898         DerivedMesh *dmesh_src;
899         BVHTreeFromMesh tree_mesh_vertices_src, tree_mesh_faces_src = {NULL};
900         MDeformVert **dv_array_src, **dv_array_dst, **dv_src, **dv_dst;
901         MVert *mv_dst, *mv_src;
902         MFace *mface_src, *mf;
903         BVHTreeNearest nearest;
904         MDeformWeight *dw_dst, *dw_src;
905         int dv_tot_src, dv_tot_dst, i, v_index, index_dst, index_src, index_nearest, index_nearest_vertex;
906         unsigned int f_index;
907         float weight, tmp_weight[4], tmp_co[3], normal[3], tmp_mat[4][4], dist_v1, dist_v2, dist_v3, dist_v4;
908         const int use_vert_sel = vertex_group_use_vert_sel(ob_dst);
909         bool is_dg_dst_new = false;
910
911         /* Ensure vertex group on target.*/
912         if ((dg_dst = defgroup_find_name(ob_dst, dg_src->name)) == NULL) {
913                 dg_dst = BKE_defgroup_new(ob_dst, dg_src->name);
914                 is_dg_dst_new = true;
915         }
916
917         /* Get meshes.*/
918         dmesh_src = mesh_get_derived_final(scene, ob_src, CD_MASK_BAREMESH | CD_MASK_MDEFORMVERT);
919         me_dst = ob_dst->data;
920
921         /* Get vertex group array from source mesh */
922         if (!ed_vgroup_dm_parray_alloc(dmesh_src, &dv_array_src, &dv_tot_src)) {
923                 BKE_report(op->reports, RPT_ERROR, "Transfer failed (source mesh does not have any vertex groups)");
924                 return false;
925         }
926
927         /* Create data in memory when nothing there.*/
928         if (!me_dst->dvert) ED_vgroup_data_create(&me_dst->id);
929
930         /* Get vertex group for destination mesh */
931         ED_vgroup_parray_alloc(&me_dst->id, &dv_array_dst, &dv_tot_dst, use_vert_sel);
932
933         /* Get indexes of vertex groups.*/
934         index_src = BLI_findindex(&ob_src->defbase, dg_src);
935         index_dst = BLI_findindex(&ob_dst->defbase, dg_dst);
936
937         /* Get vertices.*/
938         mv_dst = me_dst->mvert;
939         mv_src = dmesh_src->getVertArray(dmesh_src);
940
941         /* Prepare transformation matrix.*/
942         invert_m4_m4(ob_src->imat, ob_src->obmat);
943         mul_m4_m4m4(tmp_mat, ob_src->imat, ob_dst->obmat);
944
945         /* Clear weights.*/
946         if (replace_mode == WT_REPLACE_ALL_WEIGHTS) {
947                 for (i = 0, dv_dst = dv_array_dst; i < me_dst->totvert; i++, dv_dst++) {
948
949                         if (*dv_dst == NULL) continue;
950
951                         dw_dst = defvert_find_index(*dv_dst, index_dst);
952                         /* Remove vertex from group.*/
953                         if (dw_dst) defvert_remove_group(*dv_dst, dw_dst);
954                 }
955         }
956
957         switch (method) {
958
959                 case WT_BY_INDEX:
960                         /* Check if indices are matching, delete and return if not.*/
961                         if (ob_dst == ob_src || dv_tot_dst == 0 || dv_tot_dst != dv_tot_src ||
962                             dv_array_src == NULL || dv_array_dst == NULL)
963                         {
964                                 if (is_dg_dst_new) {
965                                         ED_vgroup_delete(ob_dst, dg_dst);
966                                 }
967
968                                 if (dv_array_src) MEM_freeN(dv_array_src);
969                                 if (dv_array_dst) MEM_freeN(dv_array_dst);
970                                 dmesh_src->release(dmesh_src);
971                                 BKE_report(op->reports, RPT_ERROR, "Transfer failed (indices are not matching)");
972                                 return false;
973                         }
974
975                         /* Loop through the vertices.*/
976                         for (i = 0, dv_src = dv_array_src, dv_dst = dv_array_dst;
977                              i < me_dst->totvert;
978                              i++, dv_dst++, dv_src++, mv_src++, mv_dst++)
979                         {
980
981                                 if (*dv_dst == NULL) {
982                                         continue;
983                                 }
984
985                                 /* Copy weight.*/
986                                 dw_src = defvert_find_index(*dv_src, index_src);
987                                 if (dw_src && dw_src->weight) {
988                                         dw_dst = defvert_verify_index(*dv_dst, index_dst);
989                                         vgroup_transfer_weight(&dw_dst->weight, dw_src->weight, replace_mode);
990                                 }
991                         }
992                         break;
993
994                 case WT_BY_NEAREST_VERTEX:
995                         /* Make node tree.*/
996                         bvhtree_from_mesh_verts(&tree_mesh_vertices_src, dmesh_src, FLT_EPSILON, 2, 6);
997
998                         /* Loop trough vertices.*/
999                         for (i = 0, dv_dst = dv_array_dst; i < me_dst->totvert; i++, dv_dst++, mv_dst++) {
1000
1001                                 if (*dv_dst == NULL) {
1002                                         continue;
1003                                 }
1004
1005                                 /* Reset nearest.*/
1006                                 nearest.dist_sq = FLT_MAX;
1007                                 /* It is faster to start searching at the top of the tree instead of previous search result.*/
1008                                 nearest.index = -1;
1009
1010                                 /* Transform into target space.*/
1011                                 mul_v3_m4v3(tmp_co, tmp_mat, mv_dst->co);
1012
1013                                 /* Node tree accelerated search for closest vetex.*/
1014                                 BLI_bvhtree_find_nearest(tree_mesh_vertices_src.tree, tmp_co,
1015                                                          &nearest, tree_mesh_vertices_src.nearest_callback, &tree_mesh_vertices_src);
1016
1017                                 /* Copy weight that are not NULL including weight value 0. In relevant cases, existing weights are
1018                                  * overwritten prior to this. See the "Clear weights." step above.*/
1019                                 dw_src = defvert_find_index(dv_array_src[nearest.index], index_src);
1020                                 if (dw_src && dw_src->weight) {
1021                                         dw_dst = defvert_verify_index(*dv_dst, index_dst);
1022                                         vgroup_transfer_weight(&dw_dst->weight, dw_src->weight, replace_mode);
1023                                 }
1024                         }
1025
1026                         /* Free memory.*/
1027                         free_bvhtree_from_mesh(&tree_mesh_vertices_src);
1028                         break;
1029
1030                 case WT_BY_NEAREST_FACE:
1031                         /* Get faces.*/
1032                         DM_ensure_tessface(dmesh_src);
1033                         mface_src = dmesh_src->getTessFaceArray(dmesh_src);
1034
1035                         /* Make node tree.*/
1036                         bvhtree_from_mesh_faces(&tree_mesh_faces_src, dmesh_src, FLT_EPSILON, 2, 6);
1037
1038                         /* Loop through the vertices.*/
1039                         for (i = 0, dv_dst = dv_array_dst; i < me_dst->totvert; i++, dv_dst++, mv_dst++) {
1040
1041                                 if (*dv_dst == NULL) {
1042                                         continue;
1043                                 }
1044
1045                                 /* Reset nearest.*/
1046                                 nearest.dist_sq = FLT_MAX;
1047                                 /* It is faster to start searching at the top of the tree instead of previous search result.*/
1048                                 nearest.index = -1;
1049
1050                                 /* Transform into target space.*/
1051                                 mul_v3_m4v3(tmp_co, tmp_mat, mv_dst->co);
1052
1053                                 /* Node tree accelerated search for closest face.*/
1054                                 BLI_bvhtree_find_nearest(tree_mesh_faces_src.tree, tmp_co,
1055                                                          &nearest, tree_mesh_faces_src.nearest_callback, &tree_mesh_faces_src);
1056                                 index_nearest = nearest.index;
1057
1058                                 /* Project onto face.*/
1059                                 mf = &mface_src[index_nearest];
1060                                 normal_tri_v3(normal, mv_src[mf->v1].co, mv_src[mf->v2].co, mv_src[mf->v3].co);
1061                                 project_v3_plane(tmp_co, normal, mv_src[mf->v1].co);
1062
1063                                 /* Interpolate weights over face.*/
1064                                 interp_weights_face_v3(tmp_weight,
1065                                                        mv_src[mf->v1].co,
1066                                                        mv_src[mf->v2].co,
1067                                                        mv_src[mf->v3].co,
1068                                                        mf->v4 ? mv_src[mf->v4].co : NULL,
1069                                                        tmp_co);
1070
1071                                 /* Get weights from face.*/
1072                                 f_index = mf->v4 ? 3 : 2;
1073                                 weight = 0.0f;
1074                                 do {
1075                                         v_index = (&mf->v1)[f_index];
1076                                         weight += tmp_weight[f_index] * defvert_find_weight(dv_array_src[v_index], index_src);
1077                                 } while (f_index--);
1078
1079                                 /* Copy weight that are not NULL including weight value 0. In relevant cases, existing weights are
1080                                  * overwritten prior to this. See the "Clear weights." step above.*/
1081                                 if (weight > 0.0f) {
1082                                         dw_dst = defvert_verify_index(*dv_dst, index_dst);
1083                                         vgroup_transfer_weight(&dw_dst->weight, weight, replace_mode);
1084                                 }
1085                         }
1086
1087                         /* Free memory.*/
1088                         free_bvhtree_from_mesh(&tree_mesh_faces_src);
1089                         break;
1090
1091                 case WT_BY_NEAREST_VERTEX_IN_FACE:
1092                         /* Get faces.*/
1093                         DM_ensure_tessface(dmesh_src);
1094                         mface_src = dmesh_src->getTessFaceArray(dmesh_src);
1095
1096                         /* Make node tree.*/
1097                         bvhtree_from_mesh_faces(&tree_mesh_faces_src, dmesh_src, FLT_EPSILON, 2, 6);
1098
1099                         /* Loop through the vertices.*/
1100                         for (i = 0, dv_dst = dv_array_dst; i < me_dst->totvert; i++, dv_dst++, mv_dst++) {
1101
1102                                 if (*dv_dst == NULL) {
1103                                         continue;
1104                                 }
1105
1106                                 /* Reset nearest.*/
1107                                 nearest.dist_sq = FLT_MAX;
1108                                 /* It is faster to start searching at the top of the tree instead of previous search result.*/
1109                                 nearest.index = -1;
1110
1111                                 /* Transform into target space.*/
1112                                 mul_v3_m4v3(tmp_co, tmp_mat, mv_dst->co);
1113
1114                                 /* Node tree accelerated search for closest face.*/
1115                                 BLI_bvhtree_find_nearest(tree_mesh_faces_src.tree, tmp_co,
1116                                                          &nearest, tree_mesh_faces_src.nearest_callback, &tree_mesh_faces_src);
1117                                 index_nearest = nearest.index;
1118
1119                                 /* Get distances.*/
1120                                 mf = &mface_src[index_nearest];
1121                                 dist_v1 = len_squared_v3v3(tmp_co, mv_src[mf->v1].co);
1122                                 dist_v2 = len_squared_v3v3(tmp_co, mv_src[mf->v2].co);
1123                                 dist_v3 = len_squared_v3v3(tmp_co, mv_src[mf->v3].co);
1124
1125                                 /* Get closest vertex.*/
1126                                 f_index = mf->v4 ? 3 : 2;
1127                                 if (dist_v1 < dist_v2 && dist_v1 < dist_v3) index_nearest_vertex = mf->v1;
1128                                 else if (dist_v2 < dist_v3) index_nearest_vertex = mf->v2;
1129                                 else index_nearest_vertex = mf->v3;
1130                                 if (f_index == 3) {
1131                                         dist_v4 = len_squared_v3v3(tmp_co, mv_src[mf->v4].co);
1132                                         if (dist_v4 < dist_v1 && dist_v4 < dist_v2 && dist_v4 < dist_v3) {
1133                                                 index_nearest_vertex = mf->v4;
1134                                         }
1135                                 }
1136
1137                                 /* Copy weight that are not NULL including weight value 0. In relevant cases, existing weights are
1138                                  * overwritten prior to this. See the "Clear weights." step above.*/
1139                                 dw_src = defvert_find_index(dv_array_src[index_nearest_vertex], index_src);
1140                                 if (dw_src && dw_src->weight) {
1141                                         dw_dst = defvert_verify_index(*dv_dst, index_dst);
1142                                         vgroup_transfer_weight(&dw_dst->weight, dw_src->weight, replace_mode);
1143                                 }
1144                         }
1145
1146                         /* Free memory.*/
1147                         free_bvhtree_from_mesh(&tree_mesh_faces_src);
1148                         break;
1149
1150                 default:
1151                         BLI_assert(0);
1152                         break;
1153         }
1154
1155         /* Free memory.*/
1156         if (dv_array_src) MEM_freeN(dv_array_src);
1157         if (dv_array_dst) MEM_freeN(dv_array_dst);
1158         dmesh_src->release(dmesh_src);
1159
1160         return true;
1161 }
1162
1163 /***********************End weight transfer (WT)***********************************/
1164
1165 /* for Mesh in Object mode */
1166 /* allows editmode for Lattice */
1167 static void ED_vgroup_nr_vert_add(Object *ob,
1168                                   const int def_nr, const int vertnum,
1169                                   const float weight, const int assignmode)
1170 {
1171         /* add the vert to the deform group with the
1172          * specified number
1173          */
1174         MDeformVert *dvert = NULL;
1175         int tot;
1176
1177         /* get the vert */
1178         ED_vgroup_array_get(ob->data, &dvert, &tot);
1179         
1180         if (dvert == NULL)
1181                 return;
1182
1183         /* check that vertnum is valid before trying to get the relevant dvert */
1184         if ((vertnum < 0) || (vertnum >= tot))
1185                 return;
1186
1187
1188         if (dvert) {
1189                 MDeformVert *dv = &dvert[vertnum];
1190                 MDeformWeight *dw;
1191
1192                 /* Lets first check to see if this vert is
1193                  * already in the weight group -- if so
1194                  * lets update it
1195                  */
1196
1197                 dw = defvert_find_index(dv, def_nr);
1198
1199                 if (dw) {
1200                         switch (assignmode) {
1201                                 case WEIGHT_REPLACE:
1202                                         dw->weight = weight;
1203                                         break;
1204                                 case WEIGHT_ADD:
1205                                         dw->weight += weight;
1206                                         if (dw->weight >= 1.0f)
1207                                                 dw->weight = 1.0f;
1208                                         break;
1209                                 case WEIGHT_SUBTRACT:
1210                                         dw->weight -= weight;
1211                                         /* if the weight is zero or less then
1212                                          * remove the vert from the deform group
1213                                          */
1214                                         if (dw->weight <= 0.0f) {
1215                                                 defvert_remove_group(dv, dw);
1216                                         }
1217                                         break;
1218                         }
1219                 }
1220                 else {
1221                         /* if the vert wasn't in the deform group then
1222                          * we must take a different form of action ...
1223                          */
1224
1225                         switch (assignmode) {
1226                                 case WEIGHT_SUBTRACT:
1227                                         /* if we are subtracting then we don't
1228                                          * need to do anything
1229                                          */
1230                                         return;
1231
1232                                 case WEIGHT_REPLACE:
1233                                 case WEIGHT_ADD:
1234                                         /* if we are doing an additive assignment, then
1235                                          * we need to create the deform weight
1236                                          */
1237
1238                                         /* we checked if the vertex was added before so no need to test again, simply add */
1239                                         defvert_add_index_notest(dv, def_nr, weight);
1240                                         break;
1241                         }
1242                 }
1243         }
1244 }
1245
1246 /* called while not in editmode */
1247 void ED_vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
1248 {
1249         /* add the vert to the deform group with the
1250          * specified assign mode
1251          */
1252         const int def_nr = BLI_findindex(&ob->defbase, dg);
1253
1254         MDeformVert *dv = NULL;
1255         int tot;
1256
1257         /* get the deform group number, exit if
1258          * it can't be found
1259          */
1260         if (def_nr != -1) {
1261
1262                 /* if there's no deform verts then create some,
1263                  */
1264                 if (ED_vgroup_array_get(ob->data, &dv, &tot) && dv == NULL)
1265                         ED_vgroup_data_create(ob->data);
1266
1267                 /* call another function to do the work
1268                  */
1269                 ED_vgroup_nr_vert_add(ob, def_nr, vertnum, weight, assignmode);
1270         }
1271 }
1272
1273 /* mesh object mode, lattice can be in editmode */
1274 void ED_vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
1275 {
1276         /* This routine removes the vertex from the specified
1277          * deform group.
1278          */
1279
1280         /* TODO, this is slow in a loop, better pass def_nr directly, but leave for later... - campbell */
1281         const int def_nr = BLI_findindex(&ob->defbase, dg);
1282
1283         if (def_nr != -1) {
1284                 MDeformVert *dvert = NULL;
1285                 int tot;
1286
1287                 /* get the deform vertices corresponding to the
1288                  * vertnum
1289                  */
1290                 ED_vgroup_array_get(ob->data, &dvert, &tot);
1291
1292                 if (dvert) {
1293                         MDeformVert *dv = &dvert[vertnum];
1294                         MDeformWeight *dw;
1295
1296                         dw = defvert_find_index(dv, def_nr);
1297                         defvert_remove_group(dv, dw); /* dw can be NULL */
1298                 }
1299         }
1300 }
1301
1302 static float get_vert_def_nr(Object *ob, const int def_nr, const int vertnum)
1303 {
1304         MDeformVert *dv = NULL;
1305
1306         /* get the deform vertices corresponding to the vertnum */
1307         if (ob->type == OB_MESH) {
1308                 Mesh *me = ob->data;
1309
1310                 if (me->edit_btmesh) {
1311                         BMEditMesh *em = me->edit_btmesh;
1312                         const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
1313                         /* warning, this lookup is _not_ fast */
1314
1315                         if (cd_dvert_offset != -1) {
1316                                 BMVert *eve;
1317                                 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
1318                                 eve = BM_vert_at_index(em->bm, vertnum);
1319                                 dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
1320                         }
1321                         else {
1322                                 return 0.0f;
1323                         }
1324                 }
1325                 else {
1326                         if (me->dvert) {
1327                                 if (vertnum >= me->totvert) {
1328                                         return 0.0f;
1329                                 }
1330                                 dv = &me->dvert[vertnum];
1331                         }
1332                 }
1333         }
1334         else if (ob->type == OB_LATTICE) {
1335                 Lattice *lt = vgroup_edit_lattice(ob);
1336
1337                 if (lt->dvert) {
1338                         if (vertnum >= lt->pntsu * lt->pntsv * lt->pntsw) {
1339                                 return 0.0f;
1340                         }
1341                         dv = &lt->dvert[vertnum];
1342                 }
1343         }
1344         
1345         if (dv) {
1346                 MDeformWeight *dw = defvert_find_index(dv, def_nr);
1347                 if (dw) {
1348                         return dw->weight;
1349                 }
1350         }
1351
1352         return -1;
1353 }
1354
1355 float ED_vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum)
1356 {
1357         const int def_nr = BLI_findindex(&ob->defbase, dg);
1358
1359         if (def_nr == -1) {
1360                 return -1;
1361         }
1362
1363         return get_vert_def_nr(ob, def_nr, vertnum);
1364 }
1365
1366 void ED_vgroup_select_by_name(Object *ob, const char *name)
1367 {   /* note: ob->actdef==0 signals on painting to create a new one, if a bone in posemode is selected */
1368         ob->actdef = defgroup_name_index(ob, name) + 1;
1369 }
1370
1371 /********************** Operator Implementations *********************/
1372
1373 /* only in editmode */
1374 static void vgroup_select_verts(Object *ob, int select)
1375 {
1376         const int def_nr = ob->actdef - 1;
1377
1378         if (!BLI_findlink(&ob->defbase, def_nr)) {
1379                 return;
1380         }
1381
1382         if (ob->type == OB_MESH) {
1383                 Mesh *me = ob->data;
1384
1385                 if (me->edit_btmesh) {
1386                         BMEditMesh *em = me->edit_btmesh;
1387                         const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
1388
1389                         if (cd_dvert_offset != -1) {
1390                                 BMIter iter;
1391                                 BMVert *eve;
1392
1393                                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
1394                                         if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
1395                                                 MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
1396                                                 if (defvert_find_index(dv, def_nr)) {
1397                                                         BM_vert_select_set(em->bm, eve, select);
1398                                                 }
1399                                         }
1400                                 }
1401
1402                                 /* this has to be called, because this function operates on vertices only */
1403                                 if (select) EDBM_select_flush(em);  /* vertices to edges/faces */
1404                                 else EDBM_deselect_flush(em);
1405                         }
1406                 }
1407                 else {
1408                         if (me->dvert) {
1409                                 MVert *mv;
1410                                 MDeformVert *dv;
1411                                 int i;
1412
1413                                 mv = me->mvert;
1414                                 dv = me->dvert;
1415
1416                                 for (i = 0; i < me->totvert; i++, mv++, dv++) {
1417                                         if (!(mv->flag & ME_HIDE)) {
1418                                                 if (defvert_find_index(dv, def_nr)) {
1419                                                         if (select) mv->flag |=  SELECT;
1420                                                         else mv->flag &= ~SELECT;
1421                                                 }
1422                                         }
1423                                 }
1424
1425                                 paintvert_flush_flags(ob);
1426                         }
1427                 }
1428         }
1429         else if (ob->type == OB_LATTICE) {
1430                 Lattice *lt = vgroup_edit_lattice(ob);
1431                 
1432                 if (lt->dvert) {
1433                         MDeformVert *dv;
1434                         BPoint *bp, *actbp = BKE_lattice_active_point_get(lt);
1435                         int a, tot;
1436                         
1437                         dv = lt->dvert;
1438
1439                         tot = lt->pntsu * lt->pntsv * lt->pntsw;
1440                         for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
1441                                 if (defvert_find_index(dv, def_nr)) {
1442                                         if (select) bp->f1 |=  SELECT;
1443                                         else {
1444                                                 bp->f1 &= ~SELECT;
1445                                                 if (actbp && bp == actbp) lt->actbp = LT_ACTBP_NONE;
1446                                         }
1447                                 }
1448                         }
1449                 }
1450         }
1451 }
1452
1453 static void vgroup_duplicate(Object *ob)
1454 {
1455         bDeformGroup *dg, *cdg;
1456         char name[sizeof(dg->name)];
1457         MDeformWeight *dw_org, *dw_cpy;
1458         MDeformVert **dvert_array = NULL;
1459         int i, idg, icdg, dvert_tot = 0;
1460
1461         dg = BLI_findlink(&ob->defbase, (ob->actdef - 1));
1462         if (!dg)
1463                 return;
1464         
1465         if (!strstr(dg->name, "_copy")) {
1466                 BLI_snprintf(name, sizeof(name), "%s_copy", dg->name);
1467         }
1468         else {
1469                 BLI_strncpy(name, dg->name, sizeof(name));
1470         }
1471
1472         cdg = defgroup_duplicate(dg);
1473         BLI_strncpy(cdg->name, name, sizeof(cdg->name));
1474         defgroup_unique_name(cdg, ob);
1475
1476         BLI_addtail(&ob->defbase, cdg);
1477
1478         idg = (ob->actdef - 1);
1479         ob->actdef = BLI_countlist(&ob->defbase);
1480         icdg = (ob->actdef - 1);
1481
1482         /* TODO, we might want to allow only copy selected verts here? - campbell */
1483         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
1484
1485         if (dvert_array) {
1486                 for (i = 0; i < dvert_tot; i++) {
1487                         MDeformVert *dv = dvert_array[i];
1488                         dw_org = defvert_find_index(dv, idg);
1489                         if (dw_org) {
1490                                 /* defvert_verify_index re-allocs org so need to store the weight first */
1491                                 const float weight = dw_org->weight;
1492                                 dw_cpy = defvert_verify_index(dv, icdg);
1493                                 dw_cpy->weight = weight;
1494                         }
1495                 }
1496
1497                 MEM_freeN(dvert_array);
1498         }
1499 }
1500
1501 /**
1502  * Return the subset type of the Vertex Group Selection
1503  */
1504 bool *ED_vgroup_subset_from_select_type(Object *ob, eVGroupSelect subset_type, int *r_vgroup_tot, int *r_subset_count)
1505 {
1506         bool *vgroup_validmap = NULL;
1507         *r_vgroup_tot = BLI_countlist(&ob->defbase);
1508
1509         switch (subset_type) {
1510                 case WT_VGROUP_ACTIVE:
1511                 {
1512                         const int def_nr_active = ob->actdef - 1;
1513                         vgroup_validmap = MEM_mallocN(*r_vgroup_tot * sizeof(*vgroup_validmap), __func__);
1514                         memset(vgroup_validmap, false, *r_vgroup_tot * sizeof(*vgroup_validmap));
1515                         if ((def_nr_active >= 0) && (def_nr_active < *r_vgroup_tot)) {
1516                                 *r_subset_count = 1;
1517                                 vgroup_validmap[def_nr_active] = true;
1518                         }
1519                         else {
1520                                 *r_subset_count = 0;
1521                         }
1522                         break;
1523                 }
1524                 case WT_VGROUP_BONE_SELECT:
1525                 {
1526                         vgroup_validmap = BKE_objdef_selected_get(ob, *r_vgroup_tot, r_subset_count);
1527                         break;
1528                 }
1529                 case WT_VGROUP_BONE_DEFORM:
1530                 {
1531                         int i;
1532                         vgroup_validmap = BKE_objdef_validmap_get(ob, *r_vgroup_tot);
1533                         *r_subset_count = 0;
1534                         for (i = 0; i < *r_vgroup_tot; i++) {
1535                                 if (vgroup_validmap[i] == true) {
1536                                         *r_subset_count += 1;
1537                                 }
1538                         }
1539                         break;
1540                 }
1541                 case WT_VGROUP_BONE_DEFORM_OFF:
1542                 {
1543                         int i;
1544                         vgroup_validmap = BKE_objdef_validmap_get(ob, *r_vgroup_tot);
1545                         *r_subset_count = 0;
1546                         for (i = 0; i < *r_vgroup_tot; i++) {
1547                                 vgroup_validmap[i] = !vgroup_validmap[i];
1548                                 if (vgroup_validmap[i] == true) {
1549                                         *r_subset_count += 1;
1550                                 }
1551                         }
1552                         break;
1553                 }
1554                 case WT_VGROUP_ALL:
1555                 default:
1556                 {
1557                         vgroup_validmap = MEM_mallocN(*r_vgroup_tot * sizeof(*vgroup_validmap), __func__);
1558                         memset(vgroup_validmap, true, *r_vgroup_tot * sizeof(*vgroup_validmap));
1559                         *r_subset_count = *r_vgroup_tot;
1560                         break;
1561                 }
1562         }
1563
1564         return vgroup_validmap;
1565 }
1566
1567 /**
1568  * store indices from the vgroup_validmap (faster lookups in some cases)
1569  */
1570 void ED_vgroup_subset_to_index_array(const bool *vgroup_validmap, const int vgroup_tot,
1571                                      int *r_vgroup_subset_map)
1572 {
1573         int i, j = 0;
1574         for (i = 0; i < vgroup_tot; i++) {
1575                 if (vgroup_validmap[i]) {
1576                         r_vgroup_subset_map[j++] = i;
1577                 }
1578         }
1579 }
1580
1581 static void vgroup_normalize(Object *ob)
1582 {
1583         MDeformWeight *dw;
1584         MDeformVert *dv, **dvert_array = NULL;
1585         int i, dvert_tot = 0;
1586         const int def_nr = ob->actdef - 1;
1587
1588         const int use_vert_sel = vertex_group_use_vert_sel(ob);
1589
1590         if (!BLI_findlink(&ob->defbase, def_nr)) {
1591                 return;
1592         }
1593
1594         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1595
1596         if (dvert_array) {
1597                 float weight_max = 0.0f;
1598
1599                 for (i = 0; i < dvert_tot; i++) {
1600
1601                         /* in case its not selected */
1602                         if (!(dv = dvert_array[i])) {
1603                                 continue;
1604                         }
1605
1606                         dw = defvert_find_index(dv, def_nr);
1607                         if (dw) {
1608                                 weight_max = max_ff(dw->weight, weight_max);
1609                         }
1610                 }
1611
1612                 if (weight_max > 0.0f) {
1613                         for (i = 0; i < dvert_tot; i++) {
1614                                 
1615                                 /* in case its not selected */
1616                                 if (!(dv = dvert_array[i])) {
1617                                         continue;
1618                                 }
1619
1620                                 dw = defvert_find_index(dv, def_nr);
1621                                 if (dw) {
1622                                         dw->weight /= weight_max;
1623                                         
1624                                         /* in case of division errors with very low weights */
1625                                         CLAMP(dw->weight, 0.0f, 1.0f);
1626                                 }
1627                         }
1628                 }
1629
1630                 MEM_freeN(dvert_array);
1631         }
1632 }
1633
1634 /* This finds all of the vertices face-connected to vert by an edge and returns a
1635  * MEM_allocated array of indices of size count.
1636  * count is an int passed by reference so it can be assigned the value of the length here. */
1637 static int *getSurroundingVerts(Mesh *me, int vert, int *count)
1638 {
1639         MPoly *mp = me->mpoly;
1640         int i = me->totpoly;
1641         /* Instead of looping twice on all polys and loops, and use a temp array, let's rather
1642          * use a BLI_array, with a reasonable starting/reserved size (typically, there are not
1643          * many vertices face-linked to another one, even 8 might be too high...). */
1644         int *verts = NULL;
1645         BLI_array_declare(verts);
1646
1647         BLI_array_reserve(verts, 8);
1648         while (i--) {
1649                 int j = mp->totloop;
1650                 int first_l = mp->totloop - 1;
1651                 MLoop *ml = &me->mloop[mp->loopstart];
1652                 while (j--) {
1653                         /* XXX This assume a vert can only be once in a poly, even though
1654                          *     it seems logical to me, not totally sure of that. */
1655                         if (ml->v == vert) {
1656                                 int a, b, k;
1657                                 if (j == first_l) {
1658                                         /* We are on the first corner. */
1659                                         a = ml[1].v;
1660                                         b = ml[j].v;
1661                                 }
1662                                 else if (!j) {
1663                                         /* We are on the last corner. */
1664                                         a = (ml - 1)->v;
1665                                         b = me->mloop[mp->loopstart].v;
1666                                 }
1667                                 else {
1668                                         a = (ml - 1)->v;
1669                                         b = (ml + 1)->v;
1670                                 }
1671
1672                                 /* Append a and b verts to array, if not yet present. */
1673                                 k = BLI_array_count(verts);
1674                                 /* XXX Maybe a == b is enough? */
1675                                 while (k-- && !(a == b && a == -1)) {
1676                                         if (verts[k] == a)
1677                                                 a = -1;
1678                                         else if (verts[k] == b)
1679                                                 b = -1;
1680                                 }
1681                                 if (a != -1)
1682                                         BLI_array_append(verts, a);
1683                                 if (b != -1)
1684                                         BLI_array_append(verts, b);
1685
1686                                 /* Vert found in this poly, we can go to next one! */
1687                                 break;
1688                         }
1689                         ml++;
1690                 }
1691                 mp++;
1692         }
1693
1694         /* Do not free the array! */
1695         *count = BLI_array_count(verts);
1696         return verts;
1697 }
1698
1699 /* get a single point in space by averaging a point cloud (vectors of size 3)
1700  * coord is the place the average is stored, points is the point cloud, count is the number of points in the cloud
1701  */
1702 static void getSingleCoordinate(MVert *points, int count, float coord[3])
1703 {
1704         int i;
1705         zero_v3(coord);
1706         for (i = 0; i < count; i++) {
1707                 add_v3_v3(coord, points[i].co);
1708         }
1709         mul_v3_fl(coord, 1.0f / count);
1710 }
1711
1712 /* given a plane and a start and end position,
1713  * compute the amount of vertical distance relative to the plane and store it in dists,
1714  * then get the horizontal and vertical change and store them in changes
1715  */
1716 static void getVerticalAndHorizontalChange(const float norm[3], float d, const float coord[3],
1717                                            const float start[3], float distToStart,
1718                                            float *end, float (*changes)[2], float *dists, int index)
1719 {
1720         /* A = Q - ((Q - P).N)N
1721          * D = (a * x0 + b * y0 +c * z0 + d) */
1722         float projA[3], projB[3];
1723         float plane[4];
1724
1725         plane_from_point_normal_v3(plane, coord, norm);
1726
1727         closest_to_plane_v3(projA, plane, start);
1728         closest_to_plane_v3(projB, plane, end);
1729         /* (vertical and horizontal refer to the plane's y and xz respectively)
1730          * vertical distance */
1731         dists[index] = dot_v3v3(norm, end) + d;
1732         /* vertical change */
1733         changes[index][0] = dists[index] - distToStart;
1734         //printf("vc %f %f\n", distance(end, projB, 3)-distance(start, projA, 3), changes[index][0]);
1735         /* horizontal change */
1736         changes[index][1] = len_v3v3(projA, projB);
1737 }
1738
1739 /* I need the derived mesh to be forgotten so the positions are recalculated
1740  * with weight changes (see dm_deform_recalc) */
1741 static void dm_deform_clear(DerivedMesh *dm, Object *ob)
1742 {
1743         if (ob->derivedDeform && (ob->derivedDeform) == dm) {
1744                 ob->derivedDeform->needsFree = 1;
1745                 ob->derivedDeform->release(ob->derivedDeform);
1746                 ob->derivedDeform = NULL;
1747         }
1748         else if (dm) {
1749                 dm->needsFree = 1;
1750                 dm->release(dm);
1751         }
1752 }
1753
1754 /* recalculate the deformation */
1755 static DerivedMesh *dm_deform_recalc(Scene *scene, Object *ob)
1756 {
1757         return mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1758 }
1759
1760 /* by changing nonzero weights, try to move a vertex in me->mverts with index 'index' to
1761  * distToBe distance away from the provided plane strength can change distToBe so that it moves
1762  * towards distToBe by that percentage cp changes how much the weights are adjusted
1763  * to check the distance
1764  *
1765  * index is the index of the vertex being moved
1766  * norm and d are the plane's properties for the equation: ax + by + cz + d = 0
1767  * coord is a point on the plane
1768  */
1769 static void moveCloserToDistanceFromPlane(Scene *scene, Object *ob, Mesh *me, int index, float norm[3],
1770                                           float coord[3], float d, float distToBe, float strength, float cp)
1771 {
1772         DerivedMesh *dm;
1773         MDeformWeight *dw;
1774         MVert m;
1775         MDeformVert *dvert = me->dvert + index;
1776         int totweight = dvert->totweight;
1777         float oldw = 0;
1778         float oldPos[3] = {0};
1779         float vc, hc, dist = 0.0f;
1780         int i, k;
1781         float (*changes)[2] = MEM_mallocN(sizeof(float *) * totweight * 2, "vertHorzChange");
1782         float *dists = MEM_mallocN(sizeof(float) * totweight, "distance");
1783
1784         /* track if up or down moved it closer for each bone */
1785         int *upDown = MEM_callocN(sizeof(int) * totweight, "upDownTracker");
1786
1787         int *dwIndices = MEM_callocN(sizeof(int) * totweight, "dwIndexTracker");
1788         float distToStart;
1789         int bestIndex = 0;
1790         bool wasChange;
1791         char wasUp;
1792         int lastIndex = -1;
1793         float originalDistToBe = distToBe;
1794         do {
1795                 wasChange = false;
1796                 dm = dm_deform_recalc(scene, ob);
1797                 dm->getVert(dm, index, &m);
1798                 copy_v3_v3(oldPos, m.co);
1799                 distToStart = dot_v3v3(norm, oldPos) + d;
1800
1801                 if (distToBe == originalDistToBe) {
1802                         distToBe += distToStart - distToStart * strength;
1803                 }
1804                 for (i = 0; i < totweight; i++) {
1805                         dwIndices[i] = i;
1806                         dw = (dvert->dw + i);
1807                         vc = hc = 0;
1808                         if (!dw->weight) {
1809                                 changes[i][0] = 0;
1810                                 changes[i][1] = 0;
1811                                 dists[i] = distToStart;
1812                                 continue;
1813                         }
1814                         for (k = 0; k < 2; k++) {
1815                                 if (dm) {
1816                                         dm_deform_clear(dm, ob); dm = NULL;
1817                                 }
1818                                 oldw = dw->weight;
1819                                 if (k) {
1820                                         dw->weight *= 1 + cp;
1821                                 }
1822                                 else {
1823                                         dw->weight /= 1 + cp;
1824                                 }
1825                                 if (dw->weight == oldw) {
1826                                         changes[i][0] = 0;
1827                                         changes[i][1] = 0;
1828                                         dists[i] = distToStart;
1829                                         break;
1830                                 }
1831                                 if (dw->weight > 1) {
1832                                         dw->weight = 1;
1833                                 }
1834                                 dm = dm_deform_recalc(scene, ob);
1835                                 dm->getVert(dm, index, &m);
1836                                 getVerticalAndHorizontalChange(norm, d, coord, oldPos, distToStart, m.co, changes, dists, i);
1837                                 dw->weight = oldw;
1838                                 if (!k) {
1839                                         vc = changes[i][0];
1840                                         hc = changes[i][1];
1841                                         dist = dists[i];
1842                                 }
1843                                 else {
1844                                         if (fabsf(dist - distToBe) < fabsf(dists[i] - distToBe)) {
1845                                                 upDown[i] = 0;
1846                                                 changes[i][0] = vc;
1847                                                 changes[i][1] = hc;
1848                                                 dists[i] = dist;
1849                                         }
1850                                         else {
1851                                                 upDown[i] = 1;
1852                                         }
1853                                         if (fabsf(dists[i] - distToBe) > fabsf(distToStart - distToBe)) {
1854                                                 changes[i][0] = 0;
1855                                                 changes[i][1] = 0;
1856                                                 dists[i] = distToStart;
1857                                         }
1858                                 }
1859                         }
1860                 }
1861                 /* sort the changes by the vertical change */
1862                 for (k = 0; k < totweight; k++) {
1863                         float tf;
1864                         int ti;
1865                         bestIndex = k;
1866                         for (i = k + 1; i < totweight; i++) {
1867                                 dist = dists[i];
1868
1869                                 if (fabsf(dist) > fabsf(dists[i])) {
1870                                         bestIndex = i;
1871                                 }
1872                         }
1873                         /* switch with k */
1874                         if (bestIndex != k) {
1875                                 ti = upDown[k];
1876                                 upDown[k] = upDown[bestIndex];
1877                                 upDown[bestIndex] = ti;
1878
1879                                 ti = dwIndices[k];
1880                                 dwIndices[k] = dwIndices[bestIndex];
1881                                 dwIndices[bestIndex] = ti;
1882
1883                                 tf = changes[k][0];
1884                                 changes[k][0] = changes[bestIndex][0];
1885                                 changes[bestIndex][0] = tf;
1886
1887                                 tf = changes[k][1];
1888                                 changes[k][1] = changes[bestIndex][1];
1889                                 changes[bestIndex][1] = tf;
1890
1891                                 tf = dists[k];
1892                                 dists[k] = dists[bestIndex];
1893                                 dists[bestIndex] = tf;
1894                         }
1895                 }
1896                 bestIndex = -1;
1897                 /* find the best change with an acceptable horizontal change */
1898                 for (i = 0; i < totweight; i++) {
1899                         if (fabsf(changes[i][0]) > fabsf(changes[i][1] * 2.0f)) {
1900                                 bestIndex = i;
1901                                 break;
1902                         }
1903                 }
1904                 if (bestIndex != -1) {
1905                         wasChange = true;
1906                         /* it is a good place to stop if it tries to move the opposite direction
1907                          * (relative to the plane) of last time */
1908                         if (lastIndex != -1) {
1909                                 if (wasUp != upDown[bestIndex]) {
1910                                         wasChange = false;
1911                                 }
1912                         }
1913                         lastIndex = bestIndex;
1914                         wasUp = upDown[bestIndex];
1915                         dw = (dvert->dw + dwIndices[bestIndex]);
1916                         oldw = dw->weight;
1917                         if (upDown[bestIndex]) {
1918                                 dw->weight *= 1 + cp;
1919                         }
1920                         else {
1921                                 dw->weight /= 1 + cp;
1922                         }
1923                         if (dw->weight > 1) {
1924                                 dw->weight = 1;
1925                         }
1926                         if (oldw == dw->weight) {
1927                                 wasChange = false;
1928                         }
1929                         if (dm) {
1930                                 dm_deform_clear(dm, ob); dm = NULL;
1931                         }
1932                 }
1933         } while (wasChange && ((distToStart - distToBe) / fabsf(distToStart - distToBe) ==
1934                                (dists[bestIndex] - distToBe) / fabsf(dists[bestIndex] - distToBe)));
1935
1936         MEM_freeN(upDown);
1937         MEM_freeN(changes);
1938         MEM_freeN(dists);
1939         MEM_freeN(dwIndices);
1940 }
1941
1942 /* this is used to try to smooth a surface by only adjusting the nonzero weights of a vertex 
1943  * but it could be used to raise or lower an existing 'bump.' */
1944 static void vgroup_fix(Scene *scene, Object *ob, float distToBe, float strength, float cp)
1945 {
1946         int i;
1947
1948         Mesh *me = ob->data;
1949         MVert *mvert = me->mvert;
1950         int *verts = NULL;
1951         if (!(me->editflag & ME_EDIT_PAINT_VERT_SEL))
1952                 return;
1953         for (i = 0; i < me->totvert && mvert; i++, mvert++) {
1954                 if (mvert->flag & SELECT) {
1955                         int count = 0;
1956                         if ((verts = getSurroundingVerts(me, i, &count))) {
1957                                 MVert m;
1958                                 MVert *p = MEM_callocN(sizeof(MVert) * (count), "deformedPoints");
1959                                 int k;
1960
1961                                 DerivedMesh *dm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1962                                 k = count;
1963                                 while (k--) {
1964                                         dm->getVert(dm, verts[k], &m);
1965                                         p[k] = m;
1966                                 }
1967                                 
1968                                 if (count >= 3) {
1969                                         float d /*, dist */ /* UNUSED */, mag;
1970                                         float coord[3];
1971                                         float norm[3];
1972                                         getSingleCoordinate(p, count, coord);
1973                                         dm->getVert(dm, i, &m);
1974                                         sub_v3_v3v3(norm, m.co, coord);
1975                                         mag = normalize_v3(norm);
1976                                         if (mag) { /* zeros fix */
1977                                                 d = -dot_v3v3(norm, coord);
1978                                                 /* dist = (dot_v3v3(norm, m.co) + d); */ /* UNUSED */
1979                                                 moveCloserToDistanceFromPlane(scene, ob, me, i, norm, coord, d, distToBe, strength, cp);
1980                                         }
1981                                 }
1982
1983                                 MEM_freeN(verts);
1984                                 MEM_freeN(p);
1985                         }
1986                 }
1987         }
1988 }
1989
1990 static void vgroup_levels_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot,
1991                                  const int UNUSED(subset_count),
1992                                  const float offset, const float gain)
1993 {
1994         MDeformWeight *dw;
1995         MDeformVert *dv, **dvert_array = NULL;
1996         int i, dvert_tot = 0;
1997
1998         const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1999         const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2000
2001         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2002
2003         if (dvert_array) {
2004
2005                 for (i = 0; i < dvert_tot; i++) {
2006                         int j;
2007
2008                         /* in case its not selected */
2009                         if (!(dv = dvert_array[i])) {
2010                                 continue;
2011                         }
2012
2013                         j = vgroup_tot;
2014                         while (j--) {
2015                                 if (vgroup_validmap[j]) {
2016                                         dw = defvert_find_index(dv, j);
2017                                         if (dw) {
2018                                                 dw->weight = gain * (dw->weight + offset);
2019
2020                                                 CLAMP(dw->weight, 0.0f, 1.0f);
2021                                         }
2022                                 }
2023                         }
2024                 }
2025
2026                 if (use_mirror && use_vert_sel) {
2027                         ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
2028                                                      vgroup_validmap, vgroup_tot);
2029                 }
2030
2031                 MEM_freeN(dvert_array);
2032         }
2033 }
2034
2035 static void vgroup_normalize_all(Object *ob,
2036                                  const bool *vgroup_validmap,
2037                                  const int vgroup_tot,
2038                                  const int subset_count,
2039                                  const bool lock_active)
2040 {
2041         MDeformVert *dv, **dvert_array = NULL;
2042         int i, dvert_tot = 0;
2043         const int def_nr = ob->actdef - 1;
2044
2045         const int use_vert_sel = vertex_group_use_vert_sel(ob);
2046
2047         if ((lock_active && !BLI_findlink(&ob->defbase, def_nr)) || subset_count == 0) {
2048                 return;
2049         }
2050
2051         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2052
2053         if (dvert_array) {
2054                 const int defbase_tot = BLI_countlist(&ob->defbase);
2055                 bool *lock_flags = BKE_objdef_lock_flags_get(ob, defbase_tot);
2056
2057                 if ((lock_active == true) &&
2058                     (lock_flags != NULL) &&
2059                     (def_nr < defbase_tot))
2060                 {
2061                         lock_flags[def_nr] = true;
2062                 }
2063
2064                 for (i = 0; i < dvert_tot; i++) {
2065                         /* in case its not selected */
2066                         if ((dv = dvert_array[i])) {
2067                                 if (lock_flags) {
2068                                         defvert_normalize_lock_map(dv, vgroup_validmap, vgroup_tot,
2069                                                                    lock_flags, defbase_tot);
2070                                 }
2071                                 else if (lock_active) {
2072                                         defvert_normalize_lock_single(dv, vgroup_validmap, vgroup_tot,
2073                                                                       def_nr);
2074                                 }
2075                                 else {
2076                                         defvert_normalize_subset(dv, vgroup_validmap, vgroup_tot);
2077                                 }
2078                         }
2079                 }
2080
2081                 if (lock_flags) {
2082                         MEM_freeN(lock_flags);
2083                 }
2084
2085                 MEM_freeN(dvert_array);
2086         }
2087 }
2088
2089 enum {
2090         VGROUP_TOGGLE,
2091         VGROUP_LOCK,
2092         VGROUP_UNLOCK,
2093         VGROUP_INVERT
2094 };
2095
2096 static EnumPropertyItem vgroup_lock_actions[] = {
2097         {VGROUP_TOGGLE, "TOGGLE", 0, "Toggle", "Unlock all vertex groups if there is at least one locked group, lock all in other case"},
2098         {VGROUP_LOCK, "LOCK", 0, "Lock", "Lock all vertex groups"},
2099         {VGROUP_UNLOCK, "UNLOCK", 0, "Unlock", "Unlock all vertex groups"},
2100         {VGROUP_INVERT, "INVERT", 0, "Invert", "Invert the lock state of all vertex groups"},
2101         {0, NULL, 0, NULL, NULL}
2102 };
2103
2104 static void vgroup_lock_all(Object *ob, int action)
2105 {
2106         bDeformGroup *dg;
2107
2108         if (action == VGROUP_TOGGLE) {
2109                 action = VGROUP_LOCK;
2110                 for (dg = ob->defbase.first; dg; dg = dg->next) {
2111                         if (dg->flag & DG_LOCK_WEIGHT) {
2112                                 action = VGROUP_UNLOCK;
2113                                 break;
2114                         }
2115                 }
2116         }
2117
2118         for (dg = ob->defbase.first; dg; dg = dg->next) {
2119                 switch (action) {
2120                         case VGROUP_LOCK:
2121                                 dg->flag |= DG_LOCK_WEIGHT;
2122                                 break;
2123                         case VGROUP_UNLOCK:
2124                                 dg->flag &= ~DG_LOCK_WEIGHT;
2125                                 break;
2126                         case VGROUP_INVERT:
2127                                 dg->flag ^= DG_LOCK_WEIGHT;
2128                                 break;
2129                 }
2130         }
2131 }
2132
2133 static void vgroup_invert_subset(Object *ob,
2134                                  const bool *vgroup_validmap, const int vgroup_tot,
2135                                  const int UNUSED(subset_count), const bool auto_assign, const bool auto_remove)
2136 {
2137         MDeformWeight *dw;
2138         MDeformVert *dv, **dvert_array = NULL;
2139         int i, dvert_tot = 0;
2140         const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2141         const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2142
2143         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2144
2145         if (dvert_array) {
2146                 for (i = 0; i < dvert_tot; i++) {
2147                         int j;
2148
2149                         /* in case its not selected */
2150                         if (!(dv = dvert_array[i])) {
2151                                 continue;
2152                         }
2153
2154                         j = vgroup_tot;
2155                         while (j--) {
2156
2157                                 if (vgroup_validmap[j]) {
2158                                         if (auto_assign) {
2159                                                 dw = defvert_verify_index(dv, j);
2160                                         }
2161                                         else {
2162                                                 dw = defvert_find_index(dv, j);
2163                                         }
2164
2165                                         if (dw) {
2166                                                 dw->weight = 1.0f - dw->weight;
2167                                                 CLAMP(dw->weight, 0.0f, 1.0f);
2168                                         }
2169                                 }
2170                         }
2171                 }
2172
2173                 if (use_mirror && use_vert_sel) {
2174                         ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
2175                                                      vgroup_validmap, vgroup_tot);
2176                 }
2177
2178                 if (auto_remove) {
2179                         ED_vgroup_parray_remove_zero(dvert_array, dvert_tot,
2180                                                      vgroup_validmap, vgroup_tot,
2181                                                      0.0f, false);
2182                 }
2183
2184                 MEM_freeN(dvert_array);
2185         }
2186 }
2187
2188 static void vgroup_blend_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot,
2189                                 const int subset_count,
2190                                 const float fac)
2191 {
2192         const float ifac = 1.0f - fac;
2193         MDeformVert **dvert_array = NULL;
2194         int i, dvert_tot = 0;
2195         int *vgroup_subset_map = BLI_array_alloca(vgroup_subset_map, subset_count);
2196         float *vgroup_subset_weights = BLI_array_alloca(vgroup_subset_weights, subset_count);
2197         const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2198
2199         BMEditMesh *em = BKE_editmesh_from_object(ob);
2200         BMesh *bm = em ? em->bm : NULL;
2201         Mesh  *me = em ? NULL   : ob->data;
2202
2203         MeshElemMap *emap ;
2204         int *emap_mem;
2205
2206         BLI_SMALLSTACK_DECLARE(dv_stack, MDeformVert *);
2207
2208         ED_vgroup_subset_to_index_array(vgroup_validmap, vgroup_tot, vgroup_subset_map);
2209         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
2210         memset(vgroup_subset_weights, 0, sizeof(*vgroup_subset_weights) * subset_count);
2211
2212         if (bm) {
2213                 BM_mesh_elem_table_ensure(bm, BM_VERT);
2214
2215                 emap = NULL;
2216                 emap_mem = NULL;
2217         }
2218         else {
2219                 BKE_mesh_vert_edge_map_create(&emap, &emap_mem,
2220                                               me->medge, me->totvert, me->totedge);
2221         }
2222
2223
2224         for (i = 0; i < dvert_tot; i++) {
2225                 MDeformVert *dv;
2226                 int dv_stack_tot = 0;
2227                 int j;
2228                 /* in case its not selected */
2229
2230                 if (bm) {
2231                         BMVert *v = BM_vert_at_index(bm, i);
2232                         if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
2233                                 BMIter eiter;
2234                                 BMEdge *e;
2235                                 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
2236                                         BMVert *v_other = BM_edge_other_vert(e, v);
2237                                         const int i_other = BM_elem_index_get(v_other);
2238
2239                                         if (BM_elem_flag_test(v_other, BM_ELEM_SELECT) == 0) {
2240                                                 dv = dvert_array[i_other];
2241                                                 BLI_SMALLSTACK_PUSH(dv_stack, dv);
2242                                                 dv_stack_tot++;
2243                                         }
2244                                 }
2245                         }
2246                 }
2247                 else {
2248                         MVert *v = &me->mvert[i];
2249                         if (v->flag & SELECT) {
2250                                 for (j = 0; j < emap[i].count; j++) {
2251                                         MEdge *e = &me->medge[emap[i].indices[j]];
2252                                         const int i_other = (e->v1 == i ? e->v2 : e->v1);
2253                                         MVert *v_other = &me->mvert[i_other];
2254
2255                                         if ((v_other->flag & SELECT) == 0) {
2256                                                 dv = dvert_array[i_other];
2257                                                 BLI_SMALLSTACK_PUSH(dv_stack, dv);
2258                                                 dv_stack_tot++;
2259                                         }
2260                                 }
2261                         }
2262                 }
2263
2264                 if (dv_stack_tot) {
2265                         const float dv_mul = 1.0f / (float)dv_stack_tot;
2266
2267                         /* vgroup_subset_weights is zero'd at this point */
2268                         while ((dv = BLI_SMALLSTACK_POP(dv_stack))) {
2269                                 for (j = 0; j < subset_count; j++) {
2270                                         vgroup_subset_weights[j] += dv_mul * defvert_find_weight(dv, vgroup_subset_map[j]);
2271                                 }
2272                         }
2273
2274                         dv = dvert_array[i];
2275                         for (j = 0; j < subset_count; j++) {
2276                                 MDeformWeight *dw;
2277                                 if (vgroup_subset_weights[j] > 0.0f) {
2278                                         dw = defvert_verify_index(dv, vgroup_subset_map[j]);
2279                                 }
2280                                 else {
2281                                         dw = defvert_find_index(dv, vgroup_subset_map[j]);
2282                                 }
2283
2284                                 if (dw) {
2285                                         dw->weight = (fac * vgroup_subset_weights[j]) + (ifac * dw->weight);
2286                                         CLAMP(dw->weight, 0.0f, 1.0f);
2287                                 }
2288
2289                                 /* zero for next iteration */
2290                                 vgroup_subset_weights[j] = 0.0f;
2291                         }
2292                 }
2293         }
2294
2295         if (bm) {
2296                 /* pass */
2297         }
2298         else {
2299                 MEM_freeN(emap);
2300                 MEM_freeN(emap_mem);
2301         }
2302
2303         if (dvert_array)
2304                 MEM_freeN(dvert_array);
2305
2306         /* not so efficient to get 'dvert_array' again just so unselected verts are NULL'd */
2307         if (use_mirror) {
2308                 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, true);
2309                 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
2310                                              vgroup_validmap, vgroup_tot);
2311                 if (dvert_array)
2312                         MEM_freeN(dvert_array);
2313         }
2314 }
2315
2316 static int inv_cmp_mdef_vert_weights(const void *a1, const void *a2)
2317 {
2318         /* qsort sorts in ascending order.  We want descending order to save a memcopy
2319          * so this compare function is inverted from the standard greater than comparison qsort needs.
2320          * A normal compare function is called with two pointer arguments and should return an integer less than, equal to,
2321          * or greater than zero corresponding to whether its first argument is considered less than, equal to,
2322          * or greater than its second argument.  This does the opposite. */
2323         const struct MDeformWeight *dw1 = a1, *dw2 = a2;
2324
2325         if      (dw1->weight < dw2->weight) return  1;
2326         else if (dw1->weight > dw2->weight) return -1;
2327         else if (&dw1 < &dw2)               return  1; /* compare addresses so we have a stable sort algorithm */
2328         else                                return -1;
2329 }
2330
2331 /* Used for limiting the number of influencing bones per vertex when exporting
2332  * skinned meshes.  if all_deform_weights is True, limit all deform modifiers
2333  * to max_weights regardless of type, otherwise, only limit the number of influencing bones per vertex*/
2334 static int vgroup_limit_total_subset(Object *ob,
2335                                      const bool *vgroup_validmap,
2336                                      const int vgroup_tot,
2337                                      const int subset_count,
2338                                      const int max_weights)
2339 {
2340         MDeformVert *dv, **dvert_array = NULL;
2341         int i, dvert_tot = 0;
2342         const int use_vert_sel = vertex_group_use_vert_sel(ob);
2343         int remove_tot = 0;
2344
2345         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2346
2347         if (dvert_array) {
2348                 int num_to_drop = 0;
2349
2350                 for (i = 0; i < dvert_tot; i++) {
2351
2352                         MDeformWeight *dw_temp;
2353                         int bone_count = 0, non_bone_count = 0;
2354                         int j;
2355
2356                         /* in case its not selected */
2357                         if (!(dv = dvert_array[i])) {
2358                                 continue;
2359                         }
2360
2361                         num_to_drop = subset_count - max_weights;
2362
2363                         /* first check if we even need to test further */
2364                         if (num_to_drop > 0) {
2365                                 /* re-pack dw array so that non-bone weights are first, bone-weighted verts at end
2366                                  * sort the tail, then copy only the truncated array back to dv->dw */
2367                                 dw_temp = MEM_mallocN(sizeof(MDeformWeight) * dv->totweight, __func__);
2368                                 bone_count = 0; non_bone_count = 0;
2369                                 for (j = 0; j < dv->totweight; j++) {
2370                                         if (LIKELY(dv->dw[j].def_nr < vgroup_tot) &&
2371                                             vgroup_validmap[dv->dw[j].def_nr])
2372                                         {
2373                                                 dw_temp[dv->totweight - 1 - bone_count] = dv->dw[j];
2374                                                 bone_count += 1;
2375                                         }
2376                                         else {
2377                                                 dw_temp[non_bone_count] = dv->dw[j];
2378                                                 non_bone_count += 1;
2379                                         }
2380                                 }
2381                                 BLI_assert(bone_count + non_bone_count == dv->totweight);
2382                                 num_to_drop = bone_count - max_weights;
2383                                 if (num_to_drop > 0) {
2384                                         qsort(&dw_temp[non_bone_count], bone_count, sizeof(MDeformWeight), inv_cmp_mdef_vert_weights);
2385                                         dv->totweight -= num_to_drop;
2386                                         /* Do we want to clean/normalize here? */
2387                                         MEM_freeN(dv->dw);
2388                                         dv->dw = MEM_reallocN(dw_temp, sizeof(MDeformWeight) * dv->totweight);
2389                                         remove_tot += num_to_drop;
2390                                 }
2391                                 else {
2392                                         MEM_freeN(dw_temp);
2393                                 }
2394                         }
2395
2396                 }
2397                 MEM_freeN(dvert_array);
2398
2399         }
2400
2401         return remove_tot;
2402 }
2403
2404
2405 static void vgroup_clean_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int UNUSED(subset_count),
2406                                 const float epsilon, const bool keep_single)
2407 {
2408         MDeformVert **dvert_array = NULL;
2409         int dvert_tot = 0;
2410         const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2411         const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2412
2413         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2414
2415         if (dvert_array) {
2416                 if (use_mirror && use_vert_sel) {
2417                         /* correct behavior in this case isn't well defined
2418                          * for now assume both sides are mirrored correctly,
2419                          * so cleaning one side also cleans the other */
2420                         ED_vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2421                 }
2422
2423                 ED_vgroup_parray_remove_zero(dvert_array, dvert_tot,
2424                                              vgroup_validmap, vgroup_tot,
2425                                              epsilon, keep_single);
2426
2427                 MEM_freeN(dvert_array);
2428         }
2429 }
2430
2431 static void vgroup_quantize_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int UNUSED(subset_count),
2432                                    const int steps)
2433 {
2434         MDeformVert **dvert_array = NULL;
2435         int dvert_tot = 0;
2436         const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2437         const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2438         ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2439
2440         if (dvert_array) {
2441                 const float steps_fl = steps;
2442                 MDeformVert *dv;
2443                 int i;
2444
2445                 if (use_mirror && use_vert_sel) {
2446                         ED_vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2447                 }
2448
2449                 for (i = 0; i < dvert_tot; i++) {
2450                         MDeformWeight *dw;
2451                         int j;
2452
2453                         /* in case its not selected */
2454                         if (!(dv = dvert_array[i])) {
2455                                 continue;
2456                         }
2457
2458                         for (j = 0, dw = dv->dw; j < dv->totweight; j++, dw++) {
2459                                 if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
2460                                         dw->weight = floorf((dw->weight * steps_fl) + 0.5f) / steps_fl;
2461                                         CLAMP(dw->weight, 0.0f, 1.0f);
2462                                 }
2463                         }
2464                 }
2465
2466                 MEM_freeN(dvert_array);
2467         }
2468 }
2469
2470 static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr,
2471                             const char sel, const char sel_mirr,
2472                             const int *flip_map, const int flip_map_len,
2473                             const bool mirror_weights, const bool flip_vgroups, const bool all_vgroups,
2474                             const int act_vgroup)
2475 {
2476         BLI_assert(sel || sel_mirr);
2477
2478         if (sel_mirr && sel) {
2479                 /* swap */
2480                 if (mirror_weights) {
2481                         if (all_vgroups) {
2482                                 SWAP(MDeformVert, *dvert, *dvert_mirr);
2483                         }
2484                         else {
2485                                 MDeformWeight *dw =      defvert_find_index(dvert, act_vgroup);
2486                                 MDeformWeight *dw_mirr = defvert_find_index(dvert_mirr, act_vgroup);
2487
2488                                 if (dw || dw_mirr) {
2489                                         if (dw_mirr == NULL)
2490                                                 dw_mirr = defvert_verify_index(dvert_mirr, act_vgroup);
2491                                         if (dw == NULL)
2492                                                 dw = defvert_verify_index(dvert, act_vgroup);
2493
2494                                         SWAP(float, dw->weight, dw_mirr->weight);
2495                                 }
2496                         }
2497                 }
2498
2499                 if (flip_vgroups) {
2500                         defvert_flip(dvert, flip_map, flip_map_len);
2501                         defvert_flip(dvert_mirr, flip_map, flip_map_len);
2502                 }
2503         }
2504         else {
2505                 /* dvert should always be the target, only swaps pointer */
2506                 if (sel_mirr) {
2507                         SWAP(MDeformVert *, dvert, dvert_mirr);
2508                 }
2509
2510                 if (mirror_weights) {
2511                         if (all_vgroups) {
2512                                 defvert_copy(dvert, dvert_mirr);
2513                         }
2514                         else {
2515                                 defvert_copy_index(dvert, dvert_mirr, act_vgroup);
2516                         }
2517                 }
2518
2519                 /* flip map already modified for 'all_vgroups' */
2520                 if (flip_vgroups) {
2521                         defvert_flip(dvert, flip_map, flip_map_len);
2522                 }
2523         }
2524 }
2525
2526 /* TODO, vgroup locking */
2527 /* TODO, face masking */
2528 void ED_vgroup_mirror(Object *ob,
2529                       const bool mirror_weights, const bool flip_vgroups,
2530                       const bool all_vgroups, const bool use_topology,
2531                       int *r_totmirr, int *r_totfail)
2532 {
2533
2534 #define VGROUP_MIRR_OP                                                        \
2535         dvert_mirror_op(dvert, dvert_mirr,                                    \
2536                         sel, sel_mirr,                                        \
2537                         flip_map, flip_map_len,                               \
2538                         mirror_weights, flip_vgroups,                         \
2539                         all_vgroups, def_nr                                   \
2540                         )
2541
2542         BMVert *eve, *eve_mirr;
2543         MDeformVert *dvert, *dvert_mirr;
2544         char sel, sel_mirr;
2545         int *flip_map = NULL, flip_map_len;
2546         const int def_nr = ob->actdef - 1;
2547         int totmirr = 0, totfail = 0;
2548
2549         *r_totmirr = *r_totfail = 0;
2550
2551         if ((mirror_weights == false && flip_vgroups == false) ||
2552             (BLI_findlink(&ob->defbase, def_nr) == NULL))
2553         {
2554                 return;
2555         }
2556
2557         if (flip_vgroups) {
2558                 flip_map = all_vgroups ?
2559                            defgroup_flip_map(ob, &flip_map_len, false) :
2560                            defgroup_flip_map_single(ob, &flip_map_len, false, def_nr);
2561
2562                 BLI_assert(flip_map != NULL);
2563
2564                 if (flip_map == NULL) {
2565                         /* something went wrong!, possibly no groups */
2566                         return;
2567                 }
2568         }
2569         else {
2570                 flip_map = NULL;
2571                 flip_map_len = 0;
2572         }
2573
2574         /* only the active group */
2575         if (ob->type == OB_MESH) {
2576                 Mesh *me = ob->data;
2577                 BMEditMesh *em = me->edit_btmesh;
2578
2579                 if (em) {
2580                         const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
2581                         BMIter iter;
2582
2583                         if (cd_dvert_offset == -1) {
2584                                 goto cleanup;
2585                         }
2586
2587                         EDBM_verts_mirror_cache_begin(em, 0, true, false, use_topology);
2588
2589                         /* Go through the list of editverts and assign them */
2590                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2591                                 if ((eve_mirr = EDBM_verts_mirror_get(em, eve))) {
2592                                         if (eve_mirr != eve) {
2593                                                 sel = BM_elem_flag_test(eve, BM_ELEM_SELECT);
2594                                                 sel_mirr = BM_elem_flag_test(eve_mirr, BM_ELEM_SELECT);
2595
2596                                                 if ((sel || sel_mirr) && (eve != eve_mirr)) {
2597                                                         dvert      = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
2598                                                         dvert_mirr = BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset);
2599
2600                                                         VGROUP_MIRR_OP;
2601                                                         totmirr++;
2602                                                 }
2603                                         }
2604
2605                                         /* don't use these again */
2606                                         EDBM_verts_mirror_cache_clear(em, eve);
2607                                         EDBM_verts_mirror_cache_clear(em, eve_mirr);
2608                                 }
2609                                 else {
2610                                         totfail++;
2611                                 }
2612                         }
2613                         EDBM_verts_mirror_cache_end(em);
2614                 }
2615                 else {
2616                         /* object mode / weight paint */
2617                         MVert *mv, *mv_mirr;
2618                         int vidx, vidx_mirr;
2619                         const int use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
2620
2621                         if (me->dvert == NULL) {
2622                                 goto cleanup;
2623                         }
2624
2625                         if (!use_vert_sel) {
2626                                 sel = sel_mirr = true;
2627                         }
2628
2629                         /* tag verts we have used */
2630                         for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2631                                 mv->flag &= ~ME_VERT_TMP_TAG;
2632                         }
2633
2634                         for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2635                                 if ((mv->flag & ME_VERT_TMP_TAG) == 0) {
2636                                         if ((vidx_mirr = mesh_get_x_mirror_vert(ob, vidx, use_topology)) != -1) {
2637                                                 if (vidx != vidx_mirr) {
2638                                                         mv_mirr = &me->mvert[vidx_mirr];
2639                                                         if ((mv_mirr->flag & ME_VERT_TMP_TAG) == 0) {
2640
2641                                                                 if (use_vert_sel) {
2642                                                                         sel = mv->flag & SELECT;
2643                                                                         sel_mirr = mv_mirr->flag & SELECT;
2644                                                                 }
2645
2646                                                                 if (sel || sel_mirr) {
2647                                                                         dvert = &me->dvert[vidx];
2648                                                                         dvert_mirr = &me->dvert[vidx_mirr];
2649
2650                                                                         VGROUP_MIRR_OP;
2651                                                                         totmirr++;
2652                                                                 }
2653
2654                                                                 mv->flag |= ME_VERT_TMP_TAG;
2655                                                                 mv_mirr->flag |= ME_VERT_TMP_TAG;
2656                                                         }
2657                                                 }
2658                                         }
2659                                         else {
2660                                                 totfail++;
2661                                         }
2662                                 }
2663                         }
2664                 }
2665         }
2666         else if (ob->type == OB_LATTICE) {
2667                 Lattice *lt = vgroup_edit_lattice(ob);
2668                 int i1, i2;
2669                 int u, v, w;
2670                 int pntsu_half;
2671                 /* half but found up odd value */
2672
2673                 if (lt->pntsu == 1 || lt->dvert == NULL) {
2674                         goto cleanup;
2675                 }
2676
2677                 /* unlike editmesh we know that by only looping over the first half of
2678                  * the 'u' indices it will cover all points except the middle which is
2679                  * ok in this case */
2680                 pntsu_half = lt->pntsu / 2;
2681
2682                 for (w = 0; w < lt->pntsw; w++) {
2683                         for (v = 0; v < lt->pntsv; v++) {
2684                                 for (u = 0; u < pntsu_half; u++) {
2685                                         int u_inv = (lt->pntsu - 1) - u;
2686                                         if (u != u_inv) {
2687                                                 BPoint *bp, *bp_mirr;
2688
2689                                                 i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
2690                                                 i2 = BKE_lattice_index_from_uvw(lt, u_inv, v, w);
2691
2692                                                 bp = &lt->def[i1];
2693                                                 bp_mirr = &lt->def[i2];
2694
2695                                                 sel = bp->f1 & SELECT;
2696                                                 sel_mirr = bp_mirr->f1 & SELECT;
2697
2698                                                 if (sel || sel_mirr) {
2699                                                         dvert = &lt->dvert[i1];
2700                                                         dvert_mirr = &lt->dvert[i2];
2701
2702                                                         VGROUP_MIRR_OP;
2703                                                         totmirr++;
2704                                                 }
2705                                         }
2706                                 }
2707                         }
2708                 }
2709         }
2710
2711         /* disabled, confusing when you have an active pose bone */
2712 #if 0
2713         /* flip active group index */
2714         if (flip_vgroups && flip_map[def_nr] >= 0)
2715                 ob->actdef = flip_map[def_nr] + 1;
2716 #endif
2717
2718 cleanup:
2719         *r_totmirr = totmirr;
2720         *r_totfail = totfail;
2721
2722         if (flip_map) MEM_freeN(flip_map);
2723
2724 #undef VGROUP_MIRR_OP
2725
2726 }
2727
2728 static void vgroup_remap_update_users(Object *ob, int *map)
2729 {
2730         ExplodeModifierData *emd;
2731         ModifierData *md;
2732         ParticleSystem *psys;
2733         ClothModifierData *clmd;
2734         ClothSimSettings *clsim;
2735         int a;
2736
2737         /* these cases don't use names to refer to vertex groups, so when
2738          * they get deleted the numbers get out of sync, this corrects that */
2739
2740         if (ob->soft)
2741                 ob->soft->vertgroup = map[ob->soft->vertgroup];
2742
2743         for (md = ob->modifiers.first; md; md = md->next) {
2744                 if (md->type == eModifierType_Explode) {
2745                         emd = (ExplodeModifierData *)md;
2746                         emd->vgroup = map[emd->vgroup];
2747                 }
2748                 else if (md->type == eModifierType_Cloth) {
2749                         clmd = (ClothModifierData *)md;
2750                         clsim = clmd->sim_parms;
2751
2752                         if (clsim) {
2753                                 clsim->vgroup_mass = map[clsim->vgroup_mass];
2754                                 clsim->vgroup_bend = map[clsim->vgroup_bend];
2755                                 clsim->vgroup_struct = map[clsim->vgroup_struct];
2756                         }
2757                 }
2758         }
2759
2760         for (psys = ob->particlesystem.first; psys; psys = psys->next) {
2761                 for (a = 0; a < PSYS_TOT_VG; a++)
2762                         psys->vgroup[a] = map[psys->vgroup[a]];
2763         }
2764 }
2765
2766
2767 static void vgroup_delete_update_users(Object *ob, int id)
2768 {
2769         int i, defbase_tot = BLI_countlist(&ob->defbase) + 1;
2770         int *map = MEM_mallocN(sizeof(int) * defbase_tot, "vgroup del");
2771
2772         map[id] = map[0] = 0;
2773         for (i = 1; i < id; i++) map[i] = i;
2774         for (i = id + 1; i < defbase_tot; i++) map[i] = i - 1;
2775
2776         vgroup_remap_update_users(ob, map);
2777         MEM_freeN(map);
2778 }
2779
2780
2781 static void vgroup_delete_object_mode(Object *ob, bDeformGroup *dg)
2782 {
2783         MDeformVert *dvert_array = NULL;
2784         int dvert_tot = 0;
2785         const int def_nr = BLI_findindex(&ob->defbase, dg);
2786
2787         BLI_assert(def_nr != -1);
2788
2789         ED_vgroup_array_get(ob->data, &dvert_array, &dvert_tot);
2790
2791         if (dvert_array) {
2792                 int i, j;
2793                 MDeformVert *dv;
2794                 for (i = 0, dv = dvert_array; i < dvert_tot; i++, dv++) {
2795                         MDeformWeight *dw;
2796
2797                         dw = defvert_find_index(dv, def_nr);
2798                         defvert_remove_group(dv, dw); /* dw can be NULL */
2799
2800                         /* inline, make into a function if anything else needs to do this */
2801                         for (j = 0; j < dv->totweight; j++) {
2802                                 if (dv->dw[j].def_nr > def_nr) {
2803                                         dv->dw[j].def_nr--;
2804                                 }
2805                         }
2806                         /* done */
2807                 }
2808         }
2809
2810         vgroup_delete_update_users(ob, def_nr + 1);
2811
2812         /* Remove the group */
2813         BLI_freelinkN(&ob->defbase, dg);
2814
2815         /* Update the active deform index if necessary */
2816         if (ob->actdef > def_nr)
2817                 ob->actdef--;
2818         if (ob->actdef < 1 && ob->defbase.first)
2819                 ob->actdef = 1;
2820
2821         /* remove all dverts */
2822         if (BLI_listbase_is_empty(&ob->defbase)) {
2823                 if (ob->type == OB_MESH) {
2824                         Mesh *me = ob->data;
2825                         CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
2826                         me->dvert = NULL;
2827                 }
2828                 else if (ob->type == OB_LATTICE) {
2829                         Lattice *lt = ob->data;
2830                         if (lt->dvert) {
2831                                 MEM_freeN(lt->dvert);
2832                                 lt->dvert = NULL;
2833                         }
2834                 }
2835         }
2836 }
2837
2838 /* only in editmode */
2839 /* removes from active defgroup, if allverts==0 only selected vertices */
2840 static bool vgroup_active_remove_verts(Object *ob, const bool allverts, bDeformGroup *dg)
2841 {
2842         MDeformVert *dv;
2843         const int def_nr = BLI_findindex(&ob->defbase, dg);
2844         bool changed = false;
2845
2846         if (ob->type == OB_MESH) {
2847                 Mesh *me = ob->data;
2848
2849                 if (me->edit_btmesh) {
2850                         BMEditMesh *em = me->edit_btmesh;
2851                         const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
2852
2853                         if (cd_dvert_offset != -1) {
2854                                 BMVert *eve;
2855                                 BMIter iter;
2856
2857                                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2858                                         dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
2859
2860                                         if (dv && dv->dw && (allverts || BM_elem_flag_test(eve, BM_ELEM_SELECT))) {
2861                                                 MDeformWeight *dw = defvert_find_index(dv, def_nr);
2862                                                 defvert_remove_group(dv, dw); /* dw can be NULL */
2863                                                 changed = true;
2864                                         }
2865                                 }
2866                         }
2867                 }
2868                 else {
2869                         if (me->dvert) {
2870                                 MVert *mv;
2871                                 int i;
2872
2873                                 mv = me->mvert;
2874                                 dv = me->dvert;
2875
2876                                 for (i = 0; i < me->totvert; i++, mv++, dv++) {
2877                                         if (mv->flag & SELECT) {
2878                                                 if (dv->dw && (allverts || (mv->flag & SELECT))) {
2879                                                         MDeformWeight *dw = defvert_find_index(dv, def_nr);
2880                                                         defvert_remove_group(dv, dw); /* dw can be NULL */
2881                                                         changed = true;
2882                                                 }
2883                                         }
2884                                 }
2885                         }
2886                 }
2887         }
2888         else if (ob->type == OB_LATTICE) {
2889                 Lattice *lt = vgroup_edit_lattice(ob);
2890                 
2891                 if (lt->dvert) {
2892                         BPoint *bp;
2893                         int i, tot = lt->pntsu * lt->pntsv * lt->pntsw;
2894                                 
2895                         for (i = 0, bp = lt->def; i < tot; i++, bp++) {
2896                                 if (allverts || (bp->f1 & SELECT)) {
2897                                         MDeformWeight *dw;
2898
2899                                         dv = &lt->dvert[i];
2900
2901                                         dw = defvert_find_index(dv, def_nr);
2902                                         defvert_remove_group(dv, dw); /* dw can be NULL */
2903                                         changed = true;
2904                                 }
2905                         }
2906                 }