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