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