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