svn merge -r40166:40279 ^/trunk/blender
[blender.git] / source / blender / modifiers / intern / MOD_weightvgproximity.c
index 2daa3f797c336fd97f5d221cdcb58d34f2538e69..6242c6d0f5e9f914c86624d5ef2128b7efcb67aa 100644 (file)
@@ -344,6 +344,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
        Mesh *ob_m = NULL;
 #endif
        MDeformVert *dvert = NULL;
+       MDeformWeight **dw, **tdw;
        int numVerts;
        float (*v_cos)[3] = NULL; /* The vertices coordinates. */
        Object *obr = NULL; /* Our target object. */
@@ -353,7 +354,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
        float *new_w =NULL;
        int *tidx, *indices = NULL;
        int numIdx = 0;
-       int i, j;
+       int i;
        char rel_ret = 0; /* Boolean, whether we have to release ret dm or not, when not using it! */
 
        /* Get number of verts. */
@@ -409,7 +410,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
        else
                ret = dm;
 #else
-       ret = CDDM_copy(dm);
+       ret = CDDM_copy(dm, 0);
        rel_ret = 1;
        dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
        if (dvert == NULL) {
@@ -423,22 +424,34 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
         */
        tidx = MEM_mallocN(sizeof(int) * numVerts, "WeightVGProximity Modifier, tidx");
        tw = MEM_mallocN(sizeof(float) * numVerts, "WeightVGProximity Modifier, tw");
+       tdw = MEM_mallocN(sizeof(MDeformWeight*) * numVerts, "WeightVGProximity Modifier, tdw");
        for (i = 0; i < numVerts; i++) {
-               for (j = 0; j < dvert[i].totweight; j++) {
-                       if(dvert[i].dw[j].def_nr == defgrp_idx) {
-                               tidx[numIdx] = i;
-                               tw[numIdx++] = dvert[i].dw[j].weight;
-                               break;
-                       }
+               MDeformWeight *_dw = defvert_find_index(&dvert[i], defgrp_idx);
+               if(_dw) {
+                       tidx[numIdx] = i;
+                       tw[numIdx] = _dw->weight;
+                       tdw[numIdx++] = _dw;
                }
        }
+       /* If no vertices found, return org data! */
+       if(numIdx == 0) {
+               MEM_freeN(tidx);
+               MEM_freeN(tw);
+               MEM_freeN(tdw);
+               if (rel_ret)
+                       ret->release(ret);
+               return dm;
+       }
        indices = MEM_mallocN(sizeof(int) * numIdx, "WeightVGProximity Modifier, indices");
        memcpy(indices, tidx, sizeof(int) * numIdx);
        org_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGProximity Modifier, org_w");
        new_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGProximity Modifier, new_w");
        memcpy(org_w, tw, sizeof(float) * numIdx);
+       dw = MEM_mallocN(sizeof(MDeformWeight*) * numIdx, "WeightVGProximity Modifier, dw");
+       memcpy(dw, tdw, sizeof(MDeformWeight*) * numIdx);
        MEM_freeN(tidx);
        MEM_freeN(tw);
+       MEM_freeN(tdw);
 
        /* Get our vertex coordinates. */
        v_cos = MEM_mallocN(sizeof(float[3]) * numIdx, "WeightVGProximity Modifier, v_cos");
@@ -447,7 +460,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
 
        /* Compute wanted distances. */
        if (wmd->proximity_mode == MOD_WVG_PROXIMITY_OBJECT) {
-               float dist = get_ob2ob_distance(ob, obr);
+               const float dist = get_ob2ob_distance(ob, obr);
                for(i = 0; i < numIdx; i++)
                        new_w[i] = dist;
        }
@@ -463,8 +476,8 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
                                        target_dm = CDDM_from_curve(obr);
                                else if (obr->type == OB_MESH) {
                                        Mesh *me = (Mesh*)obr->data;
-                                       if (me->edit_mesh)
-                                               target_dm = CDDM_from_editmesh((EditMesh*)me->edit_mesh, me);
+                                       if (me->edit_btmesh)
+                                               target_dm = CDDM_from_BMEditMesh(me->edit_btmesh, me, 0);
                                        else
                                                target_dm = CDDM_from_mesh(me, obr);
                                }
@@ -482,8 +495,10 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
                                                       target_dm, &loc2trgt);
                                for(i = 0; i < numIdx; i++) {
                                        new_w[i] = dists_v ? dists_v[i] : FLT_MAX;
-                                       new_w[i] = dists_e ? minf(dists_e[i], new_w[i]) : new_w[i];
-                                       new_w[i] = dists_f ? minf(dists_f[i], new_w[i]) : new_w[i];
+                                       if(dists_e)
+                                               new_w[i] = minf(dists_e[i], new_w[i]);
+                                       if(dists_f)
+                                               new_w[i] = minf(dists_f[i], new_w[i]);
                                }
                                if(dists_v) MEM_freeN(dists_v);
                                if(dists_e) MEM_freeN(dists_e);
@@ -508,11 +523,12 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
                         wmd->mask_tex_mapping, wmd->mask_tex_map_obj, wmd->mask_tex_uvlayer_name);
 
        /* Update vgroup. Note we never add nor remove vertices from vgroup here. */
-       weightvg_update_vg(dvert, defgrp_idx, numIdx, indices, org_w, 0, 0.0f, 0, 0.0f);
+       weightvg_update_vg(dvert, defgrp_idx, dw, numIdx, indices, org_w, FALSE, 0.0f, FALSE, 0.0f);
 
        /* Freeing stuff. */
        MEM_freeN(org_w);
        MEM_freeN(new_w);
+       MEM_freeN(dw);
        MEM_freeN(indices);
        MEM_freeN(v_cos);
 
@@ -521,7 +537,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
 }
 
 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
-                                    struct EditMesh *UNUSED(editData),
+                                    struct BMEditMesh *UNUSED(editData),
                                     DerivedMesh *derivedData)
 {
        return applyModifier(md, ob, derivedData, 0, 1);