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