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