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