remove duplicate variable - wpaint_make_validmap(ob) was being called twice for...
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.c
index 0f2e8b84e9f04036b9e42b9e3fad012b67203743..6cda5e07118f0227c1ea5719af4b3dcb1a2cb37e 100644 (file)
@@ -64,6 +64,7 @@
 #include "RNA_enum_types.h"
 
 #include "BKE_DerivedMesh.h"
+#include "BKE_armature.h"
 #include "BKE_action.h"
 #include "BKE_brush.h"
 #include "BKE_context.h"
@@ -398,15 +399,15 @@ void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
        unsigned char i;
        int vgroup_mirror= -1;
        int selected;
-       // Jason
-       int selectedVerts;
+       
+       int use_vert_sel;
 
        me= ob->data;
        if(me==NULL || me->totface==0 || me->dvert==NULL || !me->mface) return;
        
        selected= (me->editflag & ME_EDIT_PAINT_MASK);
-       // Jason
-       selectedVerts = (me->editflag & ME_EDIT_VERT_SEL);
+       
+       use_vert_sel= (me->editflag & ME_EDIT_VERT_SEL) != 0;
 
        indexar= get_indexarray(me);
 
@@ -441,9 +442,9 @@ void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
                        faceverts[2]= mface->v3;
                        faceverts[3]= mface->v4;
                        for (i=0; i<3 || faceverts[i]; i++) {
-                               if(!((me->dvert+faceverts[i])->flag)) {
-                                       // Jason
-                                       if(selectedVerts && !((me->mvert+faceverts[i])->flag & 1)) {
+                               if(!me->dvert[faceverts[i]].flag) {
+
+                                       if(use_vert_sel && !(me->mvert[faceverts[i]].flag & SELECT)) {
                                                continue;
                                        }
 
@@ -839,8 +840,10 @@ static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float
                if (dw->weight > paintval)
                        dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
        }
-       // Jason delay clamping until the end so multi-paint can function when the active group is at the limits
-       //CLAMP(dw->weight, 0.0f, 1.0f);
+       /*  delay clamping until the end so multi-paint can function when the active group is at the limits */
+       if(multipaint == FALSE) {
+               CLAMP(dw->weight, 0.0f, 1.0f);
+       }
        
        /* if no spray, clip result with orig weight & orig alpha */
        if((wp->flag & VP_SPRAY)==0) {
@@ -867,8 +870,9 @@ static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float
                        else
                                testw = uw->weight;
                }
-               //CLAMP(testw, 0.0f, 1.0f);
-               if(!multipaint) {
+
+               if(multipaint == FALSE) {
+                       CLAMP(testw, 0.0f, 1.0f);
                        if( testw<uw->weight ) {
                                if(dw->weight < testw) dw->weight= testw;
                                else if(dw->weight > uw->weight) dw->weight= uw->weight;
@@ -1073,6 +1077,7 @@ void PAINT_OT_weight_sample_group(wmOperatorType *ot)
 }
 
 
+#if 0 /* UNUSED */
 static void do_weight_paint_auto_normalize(MDeformVert *dvert, 
                                           int paint_nr, char *map)
 {
@@ -1107,7 +1112,9 @@ static void do_weight_paint_auto_normalize(MDeformVert *dvert,
                }
        }
 }
-// Jason was here: the active group should be involved in auto normalize
+#endif
+
+/* the active group should be involved in auto normalize */
 static void do_weight_paint_auto_normalize_all_groups(MDeformVert *dvert, char *map)
 {
 //     MDeformWeight *dw = dvert->dw;
@@ -1136,84 +1143,83 @@ static void do_weight_paint_auto_normalize_all_groups(MDeformVert *dvert, char *
                }
        }
 }
-/* Jason was here */
+
 /*
 See if the current deform vertex has a locked group
 */
-static char has_locked_group(MDeformVert *dvert, char *flags)
+static char has_locked_group(MDeformVert *dvert, const char *lock_flags)
 {
        int i;
        for(i = 0; i < dvert->totweight; i++) {
-               if(flags[(dvert->dw+i)->def_nr] && (dvert->dw+i)->weight > 0.0f) {
+               if(lock_flags[dvert->dw[i].def_nr] && dvert->dw[i].weight > 0.0f) {
                        return TRUE;
                }
        }
        return FALSE;
 }
-/*Jason was here
-gen_lck_flags gets the status of "flag" for each bDeformGroup
-in ob->defbase and returns an array containing them
-*/
-static char* gen_lck_flags(Object* ob, int defcnt, char *map)
+/* 
+ * gen_lck_flags gets the status of "flag" for each bDeformGroup
+ *in ob->defbase and returns an array containing them
+ */
+static char *gen_lock_flags(Object* ob, int defbase_tot)
 {
        char is_locked = FALSE;
        int i;
-       //int defcnt = BLI_countlist(&ob->defbase);
-       char *flags = MEM_mallocN(defcnt*sizeof(char), "defflags");
+       //int defbase_tot = BLI_countlist(&ob->defbase);
+       char *lock_flags = MEM_mallocN(defbase_tot*sizeof(char), "defflags");
        bDeformGroup *defgroup;
-       int selected = 0;
 
-       for(i = 0, defgroup = ob->defbase.first; i < defcnt && defgroup; defgroup = defgroup->next, i++) {
-               flags[i] = defgroup->flag;
-               if(flags[i]) {
-                       is_locked = TRUE;
-               }
+       for(i = 0, defgroup = ob->defbase.first; i < defbase_tot && defgroup; defgroup = defgroup->next, i++) {
+               lock_flags[i] = ((defgroup->flag & DG_LOCK_WEIGHT) != 0);
+               is_locked |= lock_flags[i];
        }
        if(is_locked){
-               return flags;
+               return lock_flags;
        }
        // don't forget to free it if it is unneeded
-       MEM_freeN(flags);
+       MEM_freeN(lock_flags);
        return NULL;
 }
-/* Jason was here */
-static int has_locked_group_selected(int defcnt, char *selection, char *flags) {
+
+static int has_locked_group_selected(int defbase_tot, char *defbase_sel, char *lock_flags) {
        int i;
-       for(i = 0; i < defcnt; i++) {
-               if(selection[i] && flags[i]) {
+       for(i = 0; i < defbase_tot; i++) {
+               if(defbase_sel[i] && lock_flags[i]) {
                        return TRUE;
                }
        }
        return FALSE;
 }
 
-/* Jason was here */
-static int has_unselected_unlocked_bone_group(int defcnt, char *selection, int selected, char *flags, char *bone_groups) {
+
+#if 0 /* UNUSED */
+static int has_unselected_unlocked_bone_group(int defbase_tot, char *defbase_sel, int selected, char *lock_flags, char *vgroup_validmap) {
        int i;
-       if(defcnt == selected) {
+       if(defbase_tot == selected) {
                return FALSE;
        }
-       for(i = 0; i < defcnt; i++) {
-               if(bone_groups[i] && !selection[i] && !flags[i]) {
+       for(i = 0; i < defbase_tot; i++) {
+               if(vgroup_validmap[i] && !defbase_sel[i] && !lock_flags[i]) {
                        return TRUE;
                }
        }
        return FALSE;
 }
+#endif
+
 
-/*Jason*/
-static void multipaint_selection(MDeformVert *dvert, float change, char *selection, int defcnt) {
+static void multipaint_selection(MDeformVert *dvert, float change, char *defbase_sel, int defbase_tot) {
        int i;
        MDeformWeight *dw;
        float val;
        // make sure they are all at most 1 after the change
-       for(i = 0; i < defcnt; i++) {
-               if(selection[i]) {
+       for(i = 0; i < defbase_tot; i++) {
+               if(defbase_sel[i]) {
                        dw = defvert_find_index(dvert, i);
                        if(dw && dw->weight) {
                                val = dw->weight * change;
                                if(val > 1) {
-                                       // Jason TODO: when the change is reduced, you need to recheck the earlier values to make sure they are not 0 (precision error)
+                                       /*  TODO: when the change is reduced, you need to recheck the earlier values to make sure they are not 0 (precision error) */
                                        change = 1.0f/dw->weight;
                                }
                                // the value should never reach zero while multi-painting if it was nonzero beforehand
@@ -1224,8 +1230,8 @@ static void multipaint_selection(MDeformVert *dvert, float change, char *selecti
                }
        }
        // apply the valid change
-       for(i = 0; i < defcnt; i++) {
-               if(selection[i]) {
+       for(i = 0; i < defbase_tot; i++) {
+               if(defbase_sel[i]) {
                        dw = defvert_find_index(dvert, i);
                        if(dw && dw->weight) {
                                dw->weight = dw->weight * change;
@@ -1233,10 +1239,10 @@ static void multipaint_selection(MDeformVert *dvert, float change, char *selecti
                }
        }
 }
-/*Jason*/
+
 // move all change onto valid, unchanged groups.  If there is change left over, then return it.
 // assumes there are valid groups to shift weight onto
-static float redistribute_change(MDeformVert *ndv, char *change_status, int changeme, int changeto, char *validmap, float totchange, float total_valid) {
+static float redistribute_change(MDeformVert *ndv, char *change_status, int changeme, int changeto, const char *vgroup_validmap, float totchange, float total_valid) {
        float was_change;
        float change;
        float oldval;
@@ -1253,7 +1259,7 @@ static float redistribute_change(MDeformVert *ndv, char *change_status, int chan
                        if(change_status[ndw->def_nr] == changeme) {
                                oldval = ndw->weight;
                                // if auto normalize is active, don't worry about upper bounds
-                               if(!validmap && ndw->weight + change > 1) {
+                               if(!vgroup_validmap && ndw->weight + change > 1) {
                                        totchange -= 1-ndw->weight;
                                        ndw->weight = 1;
                                        // stop the changes to this group
@@ -1279,11 +1285,13 @@ static float redistribute_change(MDeformVert *ndv, char *change_status, int chan
        // left overs
        return totchange;
 }
-/*Jason*/
+
 // observe the changes made to the weights of groups.
 // make sure all locked groups on the vertex have the same deformation
 // by moving the changes made to groups onto other unlocked groups
-static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defcnt, char *flags, char *bone_groups, char *validmap) {
+static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defbase_tot,
+                          const char *lock_flags, const char *vgroup_validmap)
+{
        float totchange = 0.0f;
        float totchange_allowed = 0.0f;
        float left_over;
@@ -1301,18 +1309,18 @@ static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defcnt, char *
        char *change_status;
        char new_weight_has_zero = FALSE;
 
-       if(!flags || !has_locked_group(ndv, flags)) {
+       if(!lock_flags || !has_locked_group(ndv, lock_flags)) {
                return;
        }
        // record if a group was changed, unlocked and not changed, or locked
-       change_status = MEM_callocN(sizeof(char)*defcnt, "unlocked_unchanged");
+       change_status = MEM_callocN(sizeof(char)*defbase_tot, "unlocked_unchanged");
 
-       for(i = 0; i < defcnt; i++) {
+       for(i = 0; i < defbase_tot; i++) {
                ndw = defvert_find_index(ndv, i);
                odw = defvert_find_index(odv, i);
                // the weights are zero, so we can assume a lot
                if(!ndw || !odw) {
-                       if (!flags[i] && bone_groups[i]){
+                       if (!lock_flags[i] && vgroup_validmap[i]){
                                defvert_verify_index(odv, i);
                                defvert_verify_index(ndv, i);
                                total_valid++;
@@ -1321,19 +1329,21 @@ static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defcnt, char *
                        continue;
                }
                // locked groups should not be changed
-               if(flags[i]) {
+               if(lock_flags[i]) {
                        ndw->weight = odw->weight;
-               } else if(ndw->weight != odw->weight) { // changed groups are handled here
-                       totchange += ndw->weight-odw->weight;
+               }
+               else if(ndw->weight != odw->weight) { // changed groups are handled here
+                       totchange += ndw->weight - odw->weight;
                        change_status[i] = 2; // was altered already
                        total_changed++;
                        if(ndw->weight == 0) {
                                new_weight_has_zero = TRUE;
-                       } else if(designatedw == -1){
+                       }
+                       else if(designatedw == -1){
                                designatedw = i;
                        }
                } // unchanged, unlocked bone groups are handled here
-               else if (bone_groups[i]){
+               else if (vgroup_validmap[i]){
                        totchange_allowed += ndw->weight;
                        total_valid++;
                        change_status[i] = 1; // can be altered while redistributing
@@ -1342,7 +1352,7 @@ static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defcnt, char *
        // if there was any change, redistribute it
        if(total_changed) {
                // auto normalize will allow weights to temporarily go above 1 in redistribution
-               if(validmap && total_changed < 0 && total_valid) {
+               if(vgroup_validmap && total_changed < 0 && total_valid) {
                        totchange_allowed = total_valid;
                }
                // there needs to be change allowed, or you should not bother
@@ -1351,22 +1361,24 @@ static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defcnt, char *
                        // on whether or not you are painting the weight(s) up or down
                        if(totchange < 0) {
                                totchange_allowed = total_valid - totchange_allowed;
-                       } else {
+                       }
+                       else {
                                totchange_allowed *= -1;
                        }
                        left_over = 0;
-                       if(fabs(totchange_allowed) < fabs(totchange)) {
+                       if(fabsf(totchange_allowed) < fabsf(totchange)) {
                                // this amount goes back onto the changed, unlocked weights
-                               left_over = fabs(fabs(totchange)-fabs(totchange_allowed));
+                               left_over = fabsf(fabsf(totchange) - fabsf(totchange_allowed));
                                if(totchange > 0) {
                                        left_over *= -1;
                                }
-                       }else {
+                       }
+                       else {
                                // all of the change will be permitted
                                totchange_allowed = -totchange;
                        }
                        // move the weight evenly between the allowed groups, move excess back onto the used groups based on the change
-                       totchange_allowed = redistribute_change(ndv, change_status, 1, -1, validmap, totchange_allowed, total_valid);
+                       totchange_allowed = redistribute_change(ndv, change_status, 1, -1, vgroup_validmap, totchange_allowed, total_valid);
                        left_over += totchange_allowed;
                        if(left_over) {
                                // more than one nonzero weights were changed with the same ratio, so keep them changed that way!
@@ -1383,34 +1395,35 @@ static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defcnt, char *
                                                                ndw->weight = (totchange_allowed + odw->weight + odw2->weight)/(1 + ndw2->weight/ndw->weight);
                                                                designatedw_changed = TRUE;
                                                        }
-                                                       ndw2->weight = ndw->weight*ndw2->weight/storedw;
+                                                       ndw2->weight = ndw->weight * ndw2->weight / storedw;
                                                }
                                        }
                                }
                                // a weight was changed to zero, only one weight was changed, or designatedw is still -1
                                // put weight back as evenly as possible
                                else {
-                                       redistribute_change(ndv, change_status, 2, -2, validmap, left_over, total_changed);
+                                       redistribute_change(ndv, change_status, 2, -2, vgroup_validmap, left_over, total_changed);
                                }
                        }
-               } else {
+               }
+               else {
                        // reset the weights
                        for(i = 0; i < ndv->totweight; i++) {
-                               (ndv->dw+i)->weight = (odv->dw+i)->weight;
+                               ndv->dw[i].weight = odv->dw[i].weight;
                        }
                }
        }
 
        MEM_freeN(change_status);
 }
-/*Jason*/
+
 // multi-paint's initial, potential change is computed here based on the user's stroke
-static float get_mp_change(MDeformVert *odv, char *selection, float brush_change) {
+static float get_mp_change(MDeformVert *odv, char *defbase_sel, float brush_change) {
        float selwsum = 0.0f;
        int i;
        MDeformWeight *dw;
        for(i=0; i < odv->totweight; i++) {
-               if(selection[(dw = (odv->dw+i))->def_nr]) {
+               if(defbase_sel[(dw= &odv->dw[i])->def_nr]) {
                        selwsum += dw->weight;
                }
        }
@@ -1419,7 +1432,7 @@ static float get_mp_change(MDeformVert *odv, char *selection, float brush_change
        }
        return 0.0f;
 }
-/*Jason*/
+
 // change the weights back to the wv's weights
 // it assumes you already have the correct pointer index
 static void reset_to_prev(MDeformVert *wv, MDeformVert *dv) {
@@ -1427,131 +1440,128 @@ static void reset_to_prev(MDeformVert *wv, MDeformVert *dv) {
        MDeformWeight *d;
        MDeformWeight *w;
        for(i = 0; i < dv->totweight; i++) {
-               d = dv->dw+i;
-               w = defvert_find_index(wv, d->def_nr);
-               // if there was no w when there is a d, then the old weight was 0
-               if(w) {
-                       d->weight = w->weight;
-               } else {
-                       d->weight = 0;
-               }
+               d= &dv->dw[i];
+               w= defvert_find_index(wv, d->def_nr);
+               /* if there was no w when there is a d, then the old weight was 0 */
+               d->weight = w ? w->weight : 0.0f;
        }
 }
-/* Jason */
+
 static void clamp_weights(MDeformVert *dvert) {
        int i;
        for (i = 0; i < dvert->totweight; i++) {
                CLAMP((dvert->dw+i)->weight, 0.0f, 1.0f);
        }
 }
-/*Jason*/
+
+/* struct to avoid passing many args each call to do_weight_paint_vertex()
+ * this _could_ be made a part of the operators 'WPaintData' struct, or at
+ * least a member, but for now keep its own struct, initialized on every
+ * paint stroke update - campbell */
+typedef struct WeightPaintInfo {
+
+       int defbase_tot;
+
+       /* both must add up to 'defbase_tot' */
+       int defbase_tot_sel;
+       int defbase_tot_unsel;
+
+       int vgroup_mirror; /* mirror group or -1 */
+
+       char *lock_flags;  /* boolean array for locked bones,
+                                               * length of defbase_tot */
+       char *defbase_sel; /* boolean array for selected bones,
+                                               * length of defbase_tot */
+
+       char *vgroup_validmap; /* same as WeightPaintData.vgroup_validmap,
+                               * only added here for convenience */
+
+       char do_flip;
+       char do_multipaint;
+} WeightPaintInfo;
+
 /* fresh start to make multi-paint and locking modular */
-static void apply_mp_lcks_normalize(Object *ob, Mesh *me, int index, MDeformWeight *dw, int defcnt, float change, float oldw, float neww, char *selection, int selected, char *bone_groups, char *validmap, char *flags, int multipaint) {
-       MDeformVert *dvert = me->dvert+index;
-       MDeformVert *dv = MEM_mallocN(sizeof (*(me->dvert+index)), "oldMDeformVert");
-
-       dv->dw= MEM_dupallocN(dvert->dw);
-       dv->flag = dvert->flag;
-       dv->totweight = dvert->totweight;
-       // do not multi-paint if a locked group is selected or the active group is locked
-       // !flags[dw->def_nr] helps if nothing is selected, but active group is locked
-       if(!flags || flags && !has_locked_group_selected(defcnt, selection, flags) && !flags[dw->def_nr]) {
-               if(multipaint && selected > 1) {
+/* returns TRUE if it thinks you need to reset the weights due to
+ * normalizing while multi-painting */
+static int apply_mp_locks_normalize(Mesh *me, const WeightPaintInfo *wpi,
+                                    const int index,
+                                    MDeformWeight *dw, MDeformWeight *tdw,
+                                    float change, float oldChange,
+                                    float oldw, float neww)
+{
+       MDeformVert *dv= me->dvert+index;
+       MDeformVert dv_test= {NULL};
+
+       dv_test.dw= MEM_dupallocN(dv->dw);
+       dv_test.flag = dv->flag;
+       dv_test.totweight = dv->totweight;
+       /* do not multi-paint if a locked group is selected or the active group is locked
+        * !lock_flags[dw->def_nr] helps if nothing is selected, but active group is locked */
+       if((wpi->lock_flags == NULL) || (wpi->lock_flags[dw->def_nr] == FALSE && has_locked_group_selected(wpi->defbase_tot, wpi->defbase_sel, wpi->lock_flags) == FALSE)) {
+               if(wpi->do_multipaint && wpi->defbase_tot_sel > 1) {
                        if(change && change!=1) {
-                               multipaint_selection(dvert, change, selection, defcnt);
+                               multipaint_selection(dv, change, wpi->defbase_sel, wpi->defbase_tot);
                        }
-               } else {// this lets users paint normally, but don't let them paint locked groups
+               }
+               else { /* this lets users paint normally, but don't let them paint locked groups */
                        dw->weight = neww;
                }
        }
-       clamp_weights(me->dvert+index);
+       clamp_weights(dv);
 
-       enforce_locks(dv, dvert, defcnt, flags, bone_groups, validmap);
+       enforce_locks(&dv_test, dv, wpi->defbase_tot, wpi->lock_flags, wpi->vgroup_validmap);
 
-       do_weight_paint_auto_normalize_all_groups(dvert, validmap);
-       
-       MEM_freeN(dv->dw);
-       MEM_freeN(dv);
-}
+       do_weight_paint_auto_normalize_all_groups(dv, wpi->vgroup_validmap);
 
-/* Jason was here duplicate function I used in DerivedMesh.c*/
-static char* get_selected_defgroups(Object *ob, int defcnt) {
-       bPoseChannel *chan;
-       bPose *pose;
-       bDeformGroup *defgroup;
-       //Bone *bone;
-       char *dg_flags = MEM_callocN(defcnt*sizeof(char), "dg_selected_flags");
-       int i;
-       Object *armob = ED_object_pose_armature(ob);
-
-       if(armob) {
-               pose = armob->pose;
-               for (chan=pose->chanbase.first; chan; chan=chan->next) {
-                       for (i = 0, defgroup = ob->defbase.first; i < defcnt && defgroup; defgroup = defgroup->next, i++) {
-                               if(!strcmp(defgroup->name, chan->bone->name)) {
-                                       dg_flags[i] = (chan->bone->flag & BONE_SELECTED);
+       if(oldChange && wpi->do_multipaint && wpi->defbase_tot_sel > 1) {
+               if(tdw->weight != oldw) {
+                       if(neww > oldw) {
+                               if(tdw->weight <= oldw) {
+                                       MEM_freeN(dv_test.dw);
+                                       return TRUE;
+                               }
+                       }
+                       else {
+                               if(tdw->weight >= oldw) {
+                                       MEM_freeN(dv_test.dw);
+                                       return TRUE;
                                }
                        }
                }
        }
-       // the array has whether or not the corresponding group is selected
-       return dg_flags;
-}
-/* TODO move duplicates to header */
-/* Jason was here duplicate function */
-static int count_true(char *list, int len)
-{
-       int i;
-       int cnt = 0;
-       for(i = 0; i < len; i++) {
-               if (list[i]) {
-                       cnt++;
-               }
-       }
-       return cnt;
+       MEM_freeN(dv_test.dw);
+       return FALSE;
 }
-// within the current dvert index, get the dw that is selected and has a weight above 0
-// this helps multi-paint
-static int get_first_selected_nonzero_weight(MDeformVert *dvert, char *selection) {
+
+/* within the current dvert index, get the dw that is selected and has a weight
+ * above 0, this helps multi-paint */
+static int get_first_selected_nonzero_weight(MDeformVert *dvert, char *defbase_sel) {
        int i;
        MDeformWeight *dw;
        for(i=0; i< dvert->totweight; i++) {
-               dw = dvert->dw+i;
-               if(selection[dw->def_nr] && dw->weight > 0) {
+               dw = &dvert->dw[i];
+               if(defbase_sel[dw->def_nr] && dw->weight > 0) {
                        return i;
                }
        }
        return -1;
 }
-// Jason
+
+
 static char *wpaint_make_validmap(Object *ob);
 
-static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, 
-                                  float alpha, float paintweight, int flip, 
-                                  int vgroup_mirror, char *validmap, int multipaint)
+
+static void do_weight_paint_vertex( /* vars which remain the same for every vert */
+                                   VPaint *wp, Object *ob, const WeightPaintInfo *wpi,
+                                    /* vars which change on each stroke */
+                                   int index, float alpha, float paintweight
+                                   )
 {
        Mesh *me= ob->data;
-       MDeformWeight *dw, *uw, *tdw, *tuw;
+       
+       MDeformWeight *dw, *uw;
        int vgroup= ob->actdef-1;
 
-       /* Jason was here */
-       char *flags;
-       char *bone_groups;
-       char *selection;
-       int selected;
-       float oldw;
-       float neww;
-       float testw;
-       int defcnt;
-       float change = 0;
-       int i;
-       // Need to know which groups are bone groups
-       if(validmap) {
-               bone_groups = validmap;
-       }else {
-               bone_groups = wpaint_make_validmap(ob);
-       }
-
        if(wp->flag & VP_ONLYVGROUP) {
                dw= defvert_find_index(me->dvert+index, vgroup);
                uw= defvert_find_index(wp->wpaint_prev+index, vgroup);
@@ -1562,85 +1572,120 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index,
        }
        if(dw==NULL || uw==NULL)
                return;
-       /* Jason was here */
-       flags = gen_lck_flags(ob, defcnt = BLI_countlist(&ob->defbase), bone_groups);
-       selection = get_selected_defgroups(ob, defcnt);
-       selected = count_true(selection, defcnt);
-       if(!selected && ob->actdef) {
-               selected = 1;
-       }
-       
-       oldw = dw->weight;
-       wpaint_blend(wp, dw, uw, alpha, paintweight, flip, multipaint && selected >1);
-       neww = dw->weight;
-       dw->weight = oldw;
-       // setup multi-paint
-       if(selected > 1 && multipaint) {
-               tdw = dw;
-               tuw = uw;
-               change = get_mp_change(wp->wpaint_prev+index, selection, neww-oldw);
-               if(change) {
-                       if(!tdw->weight) {
-                               i = get_first_selected_nonzero_weight(me->dvert+index, selection);
-                               if(i>=0) {
-                                       tdw = ((me->dvert+index)->dw+i);
-                                       tuw = defvert_verify_index(wp->wpaint_prev+index, tdw->def_nr);
-                               } else {
-                                       change = 0;
-                               }
+
+       /* TODO: De-duplicate the simple weight paint - jason */
+       /* ... or not, since its <10 SLOC - campbell */
+
+       /* If there are no locks or multipaint,
+        * then there is no need to run the more complicated checks */
+       if((wpi->do_multipaint == FALSE || wpi->defbase_tot_sel <= 1) && (wpi->lock_flags == NULL || has_locked_group(&me->dvert[index], wpi->lock_flags) == FALSE)) {
+               wpaint_blend(wp, dw, uw, alpha, paintweight, wpi->do_flip, FALSE);
+               do_weight_paint_auto_normalize_all_groups(me->dvert+index, wpi->vgroup_validmap);
+
+               if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
+                       int j= mesh_get_x_mirror_vert(ob, index);
+                       if(j>=0) {
+                               /* copy, not paint again */
+                               uw= defvert_verify_index(me->dvert+j, (wpi->vgroup_mirror != -1) ? wpi->vgroup_mirror : vgroup);
+                                       
+                               uw->weight= dw->weight;
+
+                               do_weight_paint_auto_normalize_all_groups(me->dvert+j, wpi->vgroup_validmap);
                        }
-                       if(change && tdw->weight * change && tuw->weight) {
-                               if(tdw->weight != tuw->weight) {
-                                       testw = tuw->weight*change;
-                                       if( testw > tuw->weight ) {
-                                               if(change > tdw->weight/tuw->weight) {
-                                                       reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
-                                               } else {
-                                                       change = 0;
+               }
+       }
+       else {
+               /* use locks and/or multipaint */
+               float oldw;
+               float neww;
+               float testw=0;
+               float change = 0;
+               float oldChange = 0;
+               int i;
+               MDeformWeight *tdw = NULL, *tuw;
+               MDeformVert dv= {NULL};
+
+               oldw = dw->weight;
+               wpaint_blend(wp, dw, uw, alpha, paintweight, wpi->do_flip, wpi->do_multipaint && wpi->defbase_tot_sel >1);
+               neww = dw->weight;
+               dw->weight = oldw;
+               
+               // setup multi-paint
+               if(wpi->defbase_tot_sel > 1 && wpi->do_multipaint) {
+                       dv.dw= MEM_dupallocN((me->dvert+index)->dw);
+                       dv.flag = me->dvert[index].flag;
+                       dv.totweight = (me->dvert+index)->totweight;
+                       tdw = dw;
+                       tuw = uw;
+                       change = get_mp_change(wp->wpaint_prev+index, wpi->defbase_sel, neww - oldw);
+                       if(change) {
+                               if(!tdw->weight) {
+                                       i = get_first_selected_nonzero_weight(me->dvert+index, wpi->defbase_sel);
+                                       if(i>=0) {
+                                               tdw = &(me->dvert[index].dw[i]);
+                                               tuw = defvert_verify_index(wp->wpaint_prev+index, tdw->def_nr);
+                                       }
+                                       else {
+                                               change = 0;
+                                       }
+                               }
+                               if(change && tuw->weight && tuw->weight * change) {
+                                       if(tdw->weight != tuw->weight) {
+                                               oldChange = tdw->weight/tuw->weight;
+                                               testw = tuw->weight*change;
+                                               if( testw > tuw->weight ) {
+                                                       if(change > oldChange) {
+                                                               /* reset the weights and use the new change */
+                                                               reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
+                                                       }
+                                                       else {
+                                                               /* the old change was more significant, so set
+                                                                * the change to 0 so that it will not do another multi-paint */
+                                                               change = 0;
+                                                       }
                                                }
-                                       } else {
-                                               if(change < tdw->weight/tuw->weight) {
-                                                       reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
-                                               } else {
-                                                       change = 0;
+                                               else {
+                                                       if(change < oldChange) {
+                                                               reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
+                                                       }
+                                                       else {
+                                                               change = 0;
+                                                       }
                                                }
                                        }
                                }
-                       } else {
-                               change = 0;
+                               else {
+                                       change = 0;
+                               }
                        }
                }
-       }
-       /* Jason was here */
-       apply_mp_lcks_normalize(ob, me, index, dw, defcnt, change, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint);
-
-       // dvert may have been altered greatly
-       dw = defvert_find_index(me->dvert+index, vgroup);
-
-       if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
-               int j= mesh_get_x_mirror_vert(ob, index);
-               if(j>=0) {
-                       /* copy, not paint again */
-                       if(vgroup_mirror != -1)
-                               uw= defvert_verify_index(me->dvert+j, vgroup_mirror);
-                       else
-                               uw= defvert_verify_index(me->dvert+j, vgroup);
-                       /* Jason */
-                       //uw->weight= dw->weight;
-                       /* Jason */
-                       apply_mp_lcks_normalize(ob, me, j, uw, defcnt, change, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint);
+               
+               if(apply_mp_locks_normalize(me, wpi, index, dw, tdw, change, oldChange, oldw, neww)) {
+                       reset_to_prev(&dv, me->dvert+index);
+                       change = 0;
+                       oldChange = 0;
+               }
+               if(dv.dw) {
+                       MEM_freeN(dv.dw);
+               }
+               /* dvert may have been altered greatly */
+               dw = defvert_find_index(me->dvert+index, vgroup);
+
+               if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
+                       int j= mesh_get_x_mirror_vert(ob, index);
+                       if(j>=0) {
+                               /* copy, not paint again */
+                               uw= defvert_verify_index(me->dvert+j, (wpi->vgroup_mirror != -1) ? wpi->vgroup_mirror : vgroup);
+                               
+                               //uw->weight= dw->weight;
+                               
+                               apply_mp_locks_normalize(me, wpi, j, uw, tdw, change, oldChange, oldw, neww);
+                       }
                }
-       }
-       /* Jason */
-       if(flags) {
-               MEM_freeN(flags);
-       }
-       MEM_freeN(selection);
-       if(!validmap) {
-               MEM_freeN(bone_groups);
        }
 }
 
+
 /* *************** set wpaint operator ****************** */
 
 static int set_wpaint(bContext *C, wmOperator *UNUSED(op))             /* toggle */
@@ -1738,10 +1783,7 @@ static char *wpaint_make_validmap(Object *ob)
 {
        bDeformGroup *dg;
        ModifierData *md;
-       char *validmap;
-       bPose *pose;
-       bPoseChannel *chan;
-       ArmatureModifierData *amd;
+       char *vgroup_validmap;
        GHash *gh = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "wpaint_make_validmap gh");
        int i = 0, step1=1;
 
@@ -1753,7 +1795,7 @@ static char *wpaint_make_validmap(Object *ob)
        if (!i)
                return NULL;
 
-       validmap = MEM_callocN(i, "wpaint valid map");
+       vgroup_validmap= MEM_callocN(i, "wpaint valid map");
 
        /*now loop through the armature modifiers and identify deform bones*/
        for (md = ob->modifiers.first; md; md= !md->next && step1 ? (step1=0), modifiers_getVirtualModifierList(ob) : md->next) {
@@ -1762,10 +1804,11 @@ static char *wpaint_make_validmap(Object *ob)
 
                if (md->type == eModifierType_Armature) 
                {
-                       amd = (ArmatureModifierData*) md;
+                       ArmatureModifierData *amd= (ArmatureModifierData*) md;
 
                        if(amd->object && amd->object->pose) {
-                               pose = amd->object->pose;
+                               bPose *pose= amd->object->pose;
+                               bPoseChannel *chan;
                                
                                for (chan=pose->chanbase.first; chan; chan=chan->next) {
                                        if (chan->bone->flag & BONE_NO_DEFORM)
@@ -1783,13 +1826,13 @@ static char *wpaint_make_validmap(Object *ob)
        /*add all names to a hash table*/
        for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
                if (BLI_ghash_lookup(gh, dg->name) != NULL) {
-                       validmap[i] = 1;
+                       vgroup_validmap[i] = TRUE;
                }
        }
 
        BLI_ghash_free(gh, NULL, NULL);
 
-       return validmap;
+       return vgroup_validmap;
 }
 
 static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
@@ -1802,7 +1845,7 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED
        struct WPaintData *wpd;
        Mesh *me;
        float mat[4][4], imat[4][4];
-
+       
        if(scene->obedit) return OPERATOR_CANCELLED;
        
        me= get_mesh(ob);
@@ -1813,18 +1856,17 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED
                ED_vgroup_data_create(&me->id);
                WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
        }
-
-
+       
        /* make mode data storage */
        wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
        paint_stroke_set_mode_data(stroke, wpd);
        view3d_set_viewcontext(C, &wpd->vc);
        wpd->vgroup_mirror= -1;
-
+       
        /*set up auto-normalize, and generate map for detecting which
          vgroups affect deform bones*/
        wpd->auto_normalize = ts->auto_normalize;
-       if (wpd->auto_normalize)
+       if (wpd->auto_normalize || ts->multipaint)
                wpd->vgroup_validmap = wpaint_make_validmap(ob);
        
        //      if(qual & LR_CTRLKEY) {
@@ -1875,7 +1917,7 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED
        if(me->editflag & ME_EDIT_MIRROR_X) {
                wpaint_mirror_vgroup_ensure(ob, &wpd->vgroup_mirror);
        }
-
+       
        return 1;
 }
 
@@ -1891,12 +1933,13 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
        float mat[4][4];
        float paintweight;
        int *indexar;
-       int totindex, index, totw, flip;
+       int totindex, index, totw;
        float alpha;
        float mval[2], pressure;
-       
-       // Jason
-       int selectedVerts;
+       int use_vert_sel;
+
+       /* intentionally dont initialize as NULL, make sure we initialize all members below */
+       WeightPaintInfo wpi;
 
        /* cannot paint if there is no stroke data */
        if (wpd == NULL) {
@@ -1916,17 +1959,38 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
        /* load projection matrix */
        mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
 
-       flip = RNA_boolean_get(itemptr, "pen_flip");
        pressure = RNA_float_get(itemptr, "pressure");
        RNA_float_get_array(itemptr, "mouse", mval);
        mval[0]-= vc->ar->winrct.xmin;
        mval[1]-= vc->ar->winrct.ymin;
-                       
+
+
+
+       /* *** setup WeightPaintInfo - pass onto do_weight_paint_vertex *** */
+       wpi.defbase_tot=        BLI_countlist(&ob->defbase);
+       wpi.defbase_sel=        MEM_mallocN(wpi.defbase_tot*sizeof(char), "wpi.defbase_sel");
+       wpi.defbase_tot_sel=    get_selected_defgroups(ob, wpi.defbase_sel, wpi.defbase_tot);
+       if(wpi.defbase_tot_sel == 0 && ob->actdef) wpi.defbase_tot_sel = 1;
+       wpi.defbase_tot_unsel=  wpi.defbase_tot - wpi.defbase_tot_sel;
+       wpi.vgroup_mirror=      wpd->vgroup_mirror;
+       wpi.lock_flags=         gen_lock_flags(ob, wpi.defbase_tot);
+       wpi.vgroup_validmap=    wpd->vgroup_validmap;
+       wpi.do_flip=            RNA_boolean_get(itemptr, "pen_flip");
+       wpi.do_multipaint=      (ts->multipaint != 0);
+       /* *** done setting up WeightPaintInfo *** */
+
+
+
        swap_m4m4(wpd->vc.rv3d->persmat, mat);
-                       
+
+       use_vert_sel= (me->editflag & ME_EDIT_VERT_SEL) != 0;
+
        /* which faces are involved */
        if(wp->flag & VP_AREA) {
+               // Ugly hack, to avoid drawing vertex index when getting the face index buffer - campbell
+               me->editflag &= ~ME_EDIT_VERT_SEL;
                totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
+               me->editflag |= use_vert_sel ? ME_EDIT_VERT_SEL : 0;
        }
        else {
                indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
@@ -1945,8 +2009,6 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                        }
                }
        }
-       // Jason
-       selectedVerts = (me->editflag & ME_EDIT_VERT_SEL);
                        
        if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
                for(index=0; index<totindex; index++) {
@@ -1971,11 +2033,19 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
        for(index=0; index<totindex; index++) {
                if(indexar[index] && indexar[index]<=me->totface) {
                        MFace *mface= me->mface + (indexar[index]-1);
-                                       
-                       (me->dvert+mface->v1)->flag= selectedVerts ? ((me->mvert+mface->v1)->flag & 1): 1;
-                       (me->dvert+mface->v2)->flag= selectedVerts ? ((me->mvert+mface->v2)->flag & 1): 1;
-                       (me->dvert+mface->v3)->flag= selectedVerts ? ((me->mvert+mface->v3)->flag & 1): 1;
-                       if(mface->v4) (me->dvert+mface->v4)->flag= selectedVerts ? ((me->mvert+mface->v4)->flag & 1): 1;
+
+                       if(use_vert_sel) {
+                               me->dvert[mface->v1].flag = (me->mvert[mface->v1].flag & SELECT);
+                               me->dvert[mface->v2].flag = (me->mvert[mface->v2].flag & SELECT);
+                               me->dvert[mface->v3].flag = (me->mvert[mface->v3].flag & SELECT);
+                               if(mface->v4) me->dvert[mface->v4].flag = (me->mvert[mface->v4].flag & SELECT);
+                       }
+                       else {
+                               me->dvert[mface->v1].flag= 1;
+                               me->dvert[mface->v2].flag= 1;
+                               me->dvert[mface->v3].flag= 1;
+                               if(mface->v4) me->dvert[mface->v4].flag= 1;
+                       }
                                        
                        if(brush->vertexpaint_tool==VP_BLUR) {
                                MDeformWeight *dw, *(*dw_func)(MDeformVert *, const int);
@@ -2005,52 +2075,31 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
        for(index=0; index<totindex; index++) {
                                
                if(indexar[index] && indexar[index]<=me->totface) {
-                       MFace *mface= me->mface + (indexar[index]-1);
-                                       
-                       if((me->dvert+mface->v1)->flag) {
-                               alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval, pressure);
-                               if(alpha) {
-                                       do_weight_paint_vertex(wp, ob, mface->v1, 
-                                               alpha, paintweight, flip, wpd->vgroup_mirror, 
-                                               wpd->vgroup_validmap, ts->multipaint);
-                               }
-                               (me->dvert+mface->v1)->flag= 0;
-                       }
-                                       
-                       if((me->dvert+mface->v2)->flag) {
-                               alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval, pressure);
-                               if(alpha) {
-                                       do_weight_paint_vertex(wp, ob, mface->v2, 
-                                               alpha, paintweight, flip, wpd->vgroup_mirror, 
-                                               wpd->vgroup_validmap, ts->multipaint);
-                               }
-                               (me->dvert+mface->v2)->flag= 0;
-                       }
-                                       
-                       if((me->dvert+mface->v3)->flag) {
-                               alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval, pressure);
-                               if(alpha) {
-                                       do_weight_paint_vertex(wp, ob, mface->v3, 
-                                               alpha, paintweight, flip, wpd->vgroup_mirror, 
-                                               wpd->vgroup_validmap, ts->multipaint);
-                               }
-                               (me->dvert+mface->v3)->flag= 0;
-                       }
-                                       
-                       if((me->dvert+mface->v4)->flag) {
-                               if(mface->v4) {
-                                       alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval, pressure);
+                       MFace *mf= me->mface + (indexar[index]-1);
+                       unsigned int fidx= mf->v4 ? 3:2;;
+                       do {
+                               int vidx= *(&mf->v1 + fidx);
+
+                               if(me->dvert[vidx].flag) {
+                                       alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*vidx, mval, pressure);
                                        if(alpha) {
-                                               do_weight_paint_vertex(wp, ob, mface->v4, 
-                                                       alpha, paintweight, flip, wpd->vgroup_mirror,
-                                                       wpd->vgroup_validmap, ts->multipaint);
+                                               do_weight_paint_vertex(wp, ob, &wpi, vidx, alpha, paintweight);
                                        }
-                                       (me->dvert+mface->v4)->flag= 0;
+                                       me->dvert[vidx].flag= 0;
                                }
-                       }
+                       } while (fidx--);
                }
        }
-                       
+
+
+       /* *** free wpi members */
+       if(wpi.lock_flags) {
+               MEM_freeN(wpi.lock_flags);
+       }
+       MEM_freeN(wpi.defbase_sel);
+       /* *** dont freeing wpi members */
+
+
        swap_m4m4(vc->rv3d->persmat, mat);
                        
        DAG_id_tag_update(ob->data, 0);
@@ -2156,7 +2205,7 @@ void PAINT_OT_weight_set(wmOperatorType *ot)
 
        /* api callbacks */
        ot->exec= weight_paint_set_exec;
-       ot->poll= mask_paint_poll; // Jason, it was facemask_paint_poll
+       ot->poll= mask_paint_poll; /* it was facemask_paint_poll */
 
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;