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