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