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