doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / blenkernel / intern / lattice.c
index e963b2e..b0f90ef 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * lattice.c
  *
  *
@@ -18,7 +18,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
 
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
+#include "BLI_utildefines.h"
 
-#include "DNA_armature_types.h"
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
-#include "DNA_modifier_types.h"
-#include "DNA_object_types.h"
 #include "DNA_scene_types.h"
+#include "DNA_object_types.h"
 #include "DNA_lattice_types.h"
 #include "DNA_curve_types.h"
 #include "DNA_key_types.h"
 
+#include "BKE_animsys.h"
 #include "BKE_anim.h"
-#include "BKE_armature.h"
-#include "BKE_curve.h"
 #include "BKE_cdderivedmesh.h"
-#include "BKE_DerivedMesh.h"
-#include "BKE_deform.h"
 #include "BKE_displist.h"
 #include "BKE_global.h"
 #include "BKE_key.h"
@@ -65,9 +61,8 @@
 #include "BKE_main.h"
 #include "BKE_mesh.h"
 #include "BKE_modifier.h"
-#include "BKE_object.h"
-#include "BKE_screen.h"
-#include "BKE_utildefines.h"
+
+#include "BKE_deform.h"
 
 //XXX #include "BIF_editdeform.h"
 
@@ -182,13 +177,13 @@ void resizelattice(Lattice *lt, int uNew, int vNew, int wNew, Object *ltOb)
        bp= lt->def;
        
        for (i=0; i<lt->pntsu*lt->pntsv*lt->pntsw; i++,bp++) {
-               VECCOPY(bp->vec, vertexCos[i]);
+               copy_v3_v3(bp->vec, vertexCos[i]);
        }
 
        MEM_freeN(vertexCos);
 }
 
-Lattice *add_lattice(char *name)
+Lattice *add_lattice(const char *name)
 {
        Lattice *lt;
        
@@ -210,10 +205,6 @@ Lattice *copy_lattice(Lattice *lt)
 
        ltn= copy_libblock(lt);
        ltn->def= MEM_dupallocN(lt->def);
-               
-#if 0 // XXX old animation system
-       id_us_plus((ID *)ltn->ipo);
-#endif // XXX old animation system
 
        ltn->key= copy_key(ltn->key);
        if(ltn->key) ltn->key->from= (ID *)ltn;
@@ -232,10 +223,20 @@ void free_lattice(Lattice *lt)
        if(lt->def) MEM_freeN(lt->def);
        if(lt->dvert) free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
        if(lt->editlatt) {
-               if(lt->editlatt->def) MEM_freeN(lt->editlatt->def);
-               if(lt->editlatt->dvert) free_dverts(lt->editlatt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
+               Lattice *editlt= lt->editlatt->latt;
+
+               if(editlt->def) MEM_freeN(editlt->def);
+               if(editlt->dvert) free_dverts(editlt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
+
+               MEM_freeN(editlt);
                MEM_freeN(lt->editlatt);
        }
+       
+       /* free animation data */
+       if (lt->adt) {
+               BKE_free_animdata(&lt->id);
+               lt->adt= NULL;
+       }
 }
 
 
@@ -250,11 +251,11 @@ void make_local_lattice(Lattice *lt)
         * - mixed: make copy
         */
        
-       if(lt->id.lib==0) return;
+       if(lt->id.lib==NULL) return;
        if(lt->id.us==1) {
-               lt->id.lib= 0;
+               lt->id.lib= NULL;
                lt->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)lt, 0);
+               new_id(NULL, (ID *)lt, NULL);
                return;
        }
        
@@ -268,9 +269,9 @@ void make_local_lattice(Lattice *lt)
        }
        
        if(local && lib==0) {
-               lt->id.lib= 0;
+               lt->id.lib= NULL;
                lt->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)lt, 0);
+               new_id(NULL, (ID *)lt, NULL);
        }
        else if(local && lib) {
                ltn= copy_lattice(lt);
@@ -280,7 +281,7 @@ void make_local_lattice(Lattice *lt)
                while(ob) {
                        if(ob->data==lt) {
                                
-                               if(ob->id.lib==0) {
+                               if(ob->id.lib==NULL) {
                                        ob->data= ltn;
                                        ltn->id.us++;
                                        lt->id.us--;
@@ -302,7 +303,7 @@ void init_latt_deform(Object *oblatt, Object *ob)
        float fu, fv, fw;
        int u, v, w;
 
-       if(lt->editlatt) lt= lt->editlatt;
+       if(lt->editlatt) lt= lt->editlatt->latt;
        bp = lt->def;
        
        fp= lt->latticedata= MEM_mallocN(sizeof(float)*3*lt->pntsu*lt->pntsv*lt->pntsw, "latticedata");
@@ -347,19 +348,29 @@ void calc_latt_deform(Object *ob, float *co, float weight)
 {
        Lattice *lt= ob->data;
        float u, v, w, tu[4], tv[4], tw[4];
-       float *fpw, *fpv, *fpu, vec[3];
+       float vec[3];
+       int idx_w, idx_v, idx_u;
        int ui, vi, wi, uu, vv, ww;
-       
-       if(lt->editlatt) lt= lt->editlatt;
+
+       /* vgroup influence */
+       int defgroup_nr= -1;
+       float co_prev[3], weight_blend= 0.0f;
+       MDeformVert *dvert= lattice_get_deform_verts(ob);
+
+
+       if(lt->editlatt) lt= lt->editlatt->latt;
        if(lt->latticedata==NULL) return;
-       
+
+       if(lt->vgroup[0] && dvert) {
+               defgroup_nr= defgroup_name_index(ob, lt->vgroup);
+               copy_v3_v3(co_prev, co);
+       }
+
        /* co is in local coords, treat with latmat */
-       
-       VECCOPY(vec, co);
-       mul_m4_v3(lt->latmat, vec);
-       
+       mul_v3_m4v3(vec, lt->latmat, co);
+
        /* u v w coords */
-       
+
        if(lt->pntsu>1) {
                u= (vec[0]-lt->fu)/lt->du;
                ui= (int)floor(u);
@@ -370,7 +381,7 @@ void calc_latt_deform(Object *ob, float *co, float weight)
                tu[0]= tu[2]= tu[3]= 0.0; tu[1]= 1.0;
                ui= 0;
        }
-       
+
        if(lt->pntsv>1) {
                v= (vec[1]-lt->fv)/lt->dv;
                vi= (int)floor(v);
@@ -381,7 +392,7 @@ void calc_latt_deform(Object *ob, float *co, float weight)
                tv[0]= tv[2]= tv[3]= 0.0; tv[1]= 1.0;
                vi= 0;
        }
-       
+
        if(lt->pntsw>1) {
                w= (vec[2]-lt->fw)/lt->dw;
                wi= (int)floor(w);
@@ -392,53 +403,58 @@ void calc_latt_deform(Object *ob, float *co, float weight)
                tw[0]= tw[2]= tw[3]= 0.0; tw[1]= 1.0;
                wi= 0;
        }
-       
+
        for(ww= wi-1; ww<=wi+2; ww++) {
                w= tw[ww-wi+1];
-               
+
                if(w!=0.0) {
                        if(ww>0) {
-                               if(ww<lt->pntsw) fpw= lt->latticedata + 3*ww*lt->pntsu*lt->pntsv;
-                               else fpw= lt->latticedata + 3*(lt->pntsw-1)*lt->pntsu*lt->pntsv;
+                               if(ww<lt->pntsw) idx_w= ww*lt->pntsu*lt->pntsv;
+                               else idx_w= (lt->pntsw-1)*lt->pntsu*lt->pntsv;
                        }
-                       else fpw= lt->latticedata;
-                       
+                       else idx_w= 0;
+
                        for(vv= vi-1; vv<=vi+2; vv++) {
                                v= w*tv[vv-vi+1];
-                               
+
                                if(v!=0.0) {
                                        if(vv>0) {
-                                               if(vv<lt->pntsv) fpv= fpw + 3*vv*lt->pntsu;
-                                               else fpv= fpw + 3*(lt->pntsv-1)*lt->pntsu;
+                                               if(vv<lt->pntsv) idx_v= idx_w + vv*lt->pntsu;
+                                               else idx_v= idx_w + (lt->pntsv-1)*lt->pntsu;
                                        }
-                                       else fpv= fpw;
-                                       
+                                       else idx_v= idx_w;
+
                                        for(uu= ui-1; uu<=ui+2; uu++) {
                                                u= weight*v*tu[uu-ui+1];
-                                               
+
                                                if(u!=0.0) {
                                                        if(uu>0) {
-                                                               if(uu<lt->pntsu) fpu= fpv + 3*uu;
-                                                               else fpu= fpv + 3*(lt->pntsu-1);
+                                                               if(uu<lt->pntsu) idx_u= idx_v + uu;
+                                                               else idx_u= idx_v + (lt->pntsu-1);
                                                        }
-                                                       else fpu= fpv;
-                                                       
-                                                       co[0]+= u*fpu[0];
-                                                       co[1]+= u*fpu[1];
-                                                       co[2]+= u*fpu[2];
+                                                       else idx_u= idx_v;
+
+                                                       madd_v3_v3fl(co, &lt->latticedata[idx_u * 3], u);
+
+                                                       if(defgroup_nr != -1)
+                                                               weight_blend += (u * defvert_find_weight(dvert + idx_u, defgroup_nr));
                                                }
                                        }
                                }
                        }
                }
        }
+
+       if(defgroup_nr != -1)
+               interp_v3_v3v3(co, co_prev, co, weight_blend);
+
 }
 
 void end_latt_deform(Object *ob)
 {
        Lattice *lt= ob->data;
        
-       if(lt->editlatt) lt= lt->editlatt;
+       if(lt->editlatt) lt= lt->editlatt->latt;
        
        if(lt->latticedata)
                MEM_freeN(lt->latticedata);
@@ -466,7 +482,9 @@ static void init_curve_deform(Object *par, Object *ob, CurveDeform *cd, int dloc
                invert_m4_m4(par->imat, par->obmat);
                mul_v3_m4v3(cd->dloc, par->imat, ob->obmat[3]);
        }
-       else cd->dloc[0]=cd->dloc[1]=cd->dloc[2]= 0.0f;
+       else {
+               cd->dloc[0]=cd->dloc[1]=cd->dloc[2]= 0.0f;
+       }
        
        cd->no_rot_axis= 0;
 }
@@ -490,7 +508,7 @@ static int where_on_path_deform(Object *ob, float ctime, float *vec, float *dir,
        else ctime1= ctime;
        
        /* vec needs 4 items */
-       if(where_on_path(ob, ctime1, vec, dir, quat, radius)) {
+       if(where_on_path(ob, ctime1, vec, dir, quat, radius, NULL)) {
                
                if(cycl==0) {
                        Path *path= cu->path;
@@ -499,16 +517,17 @@ static int where_on_path_deform(Object *ob, float ctime, float *vec, float *dir,
                        if(ctime < 0.0) {
                                sub_v3_v3v3(dvec, path->data[1].vec, path->data[0].vec);
                                mul_v3_fl(dvec, ctime*(float)path->len);
-                               VECADD(vec, vec, dvec);
-                               if(quat) QUATCOPY(quat, path->data[0].quat);
+                               add_v3_v3(vec, dvec);
+                               if(quat) copy_qt_qt(quat, path->data[0].quat);
                                if(radius) *radius= path->data[0].radius;
                        }
                        else if(ctime > 1.0) {
                                sub_v3_v3v3(dvec, path->data[path->len-1].vec, path->data[path->len-2].vec);
                                mul_v3_fl(dvec, (ctime-1.0)*(float)path->len);
-                               VECADD(vec, vec, dvec);
-                               if(quat) QUATCOPY(quat, path->data[path->len-1].quat);
+                               add_v3_v3(vec, dvec);
+                               if(quat) copy_qt_qt(quat, path->data[path->len-1].quat);
                                if(radius) *radius= path->data[path->len-1].radius;
+                               /* weight - not used but could be added */
                        }
                }
                return 1;
@@ -584,22 +603,12 @@ static int calc_curve_deform(Scene *scene, Object *par, float *co, short axis, C
                }
 #endif
 
-
-               static float q_x90d[4] = {0.70710676908493, 0.70710676908493, 0.0, 0.0};        // float rot_axis[3]= {1,0,0}; axis_angle_to_quat(q, rot_axis, 90 * (M_PI / 180));
-               static float q_y90d[4] = {0.70710676908493, 0.0, 0.70710676908493, 0.0};        // float rot_axis[3]= {0,1,0}; axis_angle_to_quat(q, rot_axis, 90 * (M_PI / 180));
-               static float q_z90d[4] = {0.70710676908493, 0.0, 0.0, 0.70710676908493};        // float rot_axis[3]= {0,0,2}; axis_angle_to_quat(q, rot_axis, 90 * (M_PI / 180));
-
-               static float q_nx90d[4] = {0.70710676908493, -0.70710676908493, 0.0, 0.0};      // float rot_axis[3]= {1,0,0}; axis_angle_to_quat(q, rot_axis, -90 * (M_PI / 180));
-               static float q_ny90d[4] = {0.70710676908493, 0.0, -0.70710676908493, 0.0};      // float rot_axis[3]= {0,1,0}; axis_angle_to_quat(q, rot_axis, -90 * (M_PI / 180));
-               static float q_nz90d[4] = {0.70710676908493, 0.0, 0.0, -0.70710676908493};      // float rot_axis[3]= {0,0,2}; axis_angle_to_quat(q, rot_axis, -90 * (M_PI / 180));
-
-
                if(cd->no_rot_axis) {   /* set by caller */
 
                        /* this is not exactly the same as 2.4x, since the axis is having rotation removed rather then
                         * changing the axis before calculating the tilt but serves much the same purpose */
                        float dir_flat[3]={0,0,0}, q[4];
-                       VECCOPY(dir_flat, dir);
+                       copy_v3_v3(dir_flat, dir);
                        dir_flat[cd->no_rot_axis-1]= 0.0f;
 
                        normalize_v3(dir);
@@ -620,53 +629,18 @@ static int calc_curve_deform(Scene *scene, Object *par, float *co, short axis, C
                 * Notice X,Y,Z Up all have light colors and each ordered CCW.
                 *
                 * Now for Neg Up XYZ, the colors are all dark, and ordered clockwise - Campbell
+                *
+                * note: moved functions into quat_apply_track/vec_apply_track
                 * */
+               copy_qt_qt(quat, new_quat);
+               copy_v3_v3(cent, co);
+
+               /* zero the axis which is not used,
+                * the big block of text above now applies to these 3 lines */
+               quat_apply_track(quat, axis-1, (axis==1 || axis==3) ? 1:0); /* up flag is a dummy, set so no rotation is done */
+               vec_apply_track(cent, axis-1);
+               cent[axis < 4 ? axis-1 : axis-4]= 0.0f;
 
-               switch(axis) {
-               case MOD_CURVE_POSX:
-                       mul_qt_qtqt(quat, new_quat, q_y90d);
-
-                       cent[0]=  0.0;
-                       cent[1]=  co[2];
-                       cent[2]=  co[1];
-                       break;
-               case MOD_CURVE_NEGX:
-                       mul_qt_qtqt(quat, new_quat, q_ny90d);
-
-                       cent[0]=  0.0;
-                       cent[1]= -co[1];
-                       cent[2]=  co[2];
-
-                       break;
-               case MOD_CURVE_POSY:
-                       mul_qt_qtqt(quat, new_quat, q_x90d);
-
-                       cent[0]=  co[2];
-                       cent[1]=  0.0;
-                       cent[2]= -co[0];
-                       break;
-               case MOD_CURVE_NEGY:
-                       mul_qt_qtqt(quat, new_quat, q_nx90d);
-
-                       cent[0]= -co[0];
-                       cent[1]=  0.0;
-                       cent[2]= -co[2];
-                       break;
-               case MOD_CURVE_POSZ:
-                       mul_qt_qtqt(quat, new_quat, q_z90d);
-
-                       cent[0]=  co[1];
-                       cent[1]= -co[0];
-                       cent[2]=  0.0;
-                       break;
-               case MOD_CURVE_NEGZ:
-                       mul_qt_qtqt(quat, new_quat, q_nz90d);
-
-                       cent[0]=  co[0];
-                       cent[1]= -co[1];
-                       cent[2]=  0.0;
-                       break;
-               }
 
                /* scale if enabled */
                if(cu->flag & CU_PATH_RADIUS)
@@ -677,11 +651,11 @@ static int calc_curve_deform(Scene *scene, Object *par, float *co, short axis, C
                mul_qt_v3(quat, cent);
 
                /* translation */
-               VECADD(co, cent, loc);
+               add_v3_v3v3(co, cent, loc);
 
                if(quatp)
-                       QUATCOPY(quatp, quat);
-               
+                       copy_qt_qt(quatp, quat);
+
                return 1;
        }
        return 0;
@@ -702,7 +676,18 @@ void curve_deform_verts(Scene *scene, Object *cuOb, Object *target, DerivedMesh
        cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist
 
        init_curve_deform(cuOb, target, &cd, (cu->flag & CU_STRETCH)==0);
-               
+
+       /* dummy bounds, keep if CU_DEFORM_BOUNDS_OFF is set */
+       if(defaxis < 3) {
+               cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= 0.0f;
+               cd.dmax[0]= cd.dmax[1]= cd.dmax[2]= 1.0f;
+       }
+       else {
+               /* negative, these bounds give a good rest position */
+               cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= -1.0f;
+               cd.dmax[0]= cd.dmax[1]= cd.dmax[2]=  0.0f;
+       }
+       
        /* check whether to use vertex groups (only possible if target is a Mesh)
         * we want either a Mesh with no derived data, or derived data with
         * deformverts
@@ -717,62 +702,84 @@ void curve_deform_verts(Scene *scene, Object *cuOb, Object *target, DerivedMesh
                use_vgroups = 0;
        
        if(vgroup && vgroup[0] && use_vgroups) {
-               bDeformGroup *curdef;
                Mesh *me= target->data;
-               int index;
-               
-               /* find the group (weak loop-in-loop) */
-               for(index = 0, curdef = target->defbase.first; curdef;
-                   curdef = curdef->next, index++)
-                       if (!strcmp(curdef->name, vgroup))
-                               break;
+               int index= defgroup_name_index(target, vgroup);
 
-               if(curdef && (me->dvert || dm)) {
+               if(index != -1 && (me->dvert || dm)) {
                        MDeformVert *dvert = me->dvert;
                        float vec[3];
-                       int j;
-
-                       INIT_MINMAX(cd.dmin, cd.dmax);
-
-                       for(a = 0; a < numVerts; a++, dvert++) {
-                               if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
+                       float weight;
+       
 
-                               for(j = 0; j < dvert->totweight; j++) {
-                                       if(dvert->dw[j].def_nr == index) {
+                       if(cu->flag & CU_DEFORM_BOUNDS_OFF) {
+                               /* dummy bounds */
+                               cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= 0.0f;
+                               cd.dmax[0]= cd.dmax[1]= cd.dmax[2]= 1.0f;
+                               
+                               dvert = me->dvert;
+                               for(a = 0; a < numVerts; a++, dvert++) {
+                                       if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
+                                       weight= defvert_find_weight(dvert, index);
+       
+                                       if(weight > 0.0f) {
                                                mul_m4_v3(cd.curvespace, vertexCos[a]);
-                                               DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
-                                               break;
+                                               copy_v3_v3(vec, vertexCos[a]);
+                                               calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
+                                               interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight);
+                                               mul_m4_v3(cd.objectspace, vertexCos[a]);
                                        }
                                }
                        }
-
-                       dvert = me->dvert;
-                       for(a = 0; a < numVerts; a++, dvert++) {
-                               if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
-
-                               for(j = 0; j < dvert->totweight; j++) {
-                                       if(dvert->dw[j].def_nr == index) {
-                                               VECCOPY(vec, vertexCos[a]);
+                       else {
+                               /* set mesh min/max bounds */
+                               INIT_MINMAX(cd.dmin, cd.dmax);
+       
+                               for(a = 0; a < numVerts; a++, dvert++) {
+                                       if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
+                                       
+                                       if(defvert_find_weight(dvert, index) > 0.0f) {
+                                               mul_m4_v3(cd.curvespace, vertexCos[a]);
+                                               DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
+                                       }
+                               }
+       
+                               dvert = me->dvert;
+                               for(a = 0; a < numVerts; a++, dvert++) {
+                                       if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
+                                       
+                                       weight= defvert_find_weight(dvert, index);
+       
+                                       if(weight > 0.0f) {
+                                               copy_v3_v3(vec, vertexCos[a]);
                                                calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
-                                               interp_v3_v3v3(vertexCos[a], vertexCos[a], vec,
-                                                        dvert->dw[j].weight);
+                                               interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight);
                                                mul_m4_v3(cd.objectspace, vertexCos[a]);
-                                               break;
                                        }
                                }
                        }
                }
-       } else {
-               INIT_MINMAX(cd.dmin, cd.dmax);
-                       
-               for(a = 0; a < numVerts; a++) {
-                       mul_m4_v3(cd.curvespace, vertexCos[a]);
-                       DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
+       }
+       else {
+               if(cu->flag & CU_DEFORM_BOUNDS_OFF) {
+                       for(a = 0; a < numVerts; a++) {
+                               mul_m4_v3(cd.curvespace, vertexCos[a]);
+                               calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
+                               mul_m4_v3(cd.objectspace, vertexCos[a]);
+                       }
                }
-
-               for(a = 0; a < numVerts; a++) {
-                       calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
-                       mul_m4_v3(cd.objectspace, vertexCos[a]);
+               else {
+                       /* set mesh min max bounds */
+                       INIT_MINMAX(cd.dmin, cd.dmax);
+                               
+                       for(a = 0; a < numVerts; a++) {
+                               mul_m4_v3(cd.curvespace, vertexCos[a]);
+                               DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
+                       }
+       
+                       for(a = 0; a < numVerts; a++) {
+                               calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
+                               mul_m4_v3(cd.objectspace, vertexCos[a]);
+                       }
                }
        }
        cu->flag = flag;
@@ -794,8 +801,8 @@ void curve_deform_vector(Scene *scene, Object *cuOb, Object *target, float *orco
        init_curve_deform(cuOb, target, &cd, 0);        /* 0 no dloc */
        cd.no_rot_axis= no_rot_axis;                            /* option to only rotate for XY, for example */
        
-       VECCOPY(cd.dmin, orco);
-       VECCOPY(cd.dmax, orco);
+       copy_v3_v3(cd.dmin, orco);
+       copy_v3_v3(cd.dmax, orco);
 
        mul_m4_v3(cd.curvespace, vec);
        
@@ -813,7 +820,7 @@ void curve_deform_vector(Scene *scene, Object *cuOb, Object *target, float *orco
 }
 
 void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
-                          float (*vertexCos)[3], int numVerts, char *vgroup)
+                                                 float (*vertexCos)[3], int numVerts, char *vgroup)
 {
        int a;
        int use_vgroups;
@@ -837,26 +844,20 @@ void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
                use_vgroups = 0;
        
        if(vgroup && vgroup[0] && use_vgroups) {
-               bDeformGroup *curdef;
                Mesh *me = target->data;
-               int index = 0;
-               
-               /* find the group (weak loop-in-loop) */
-               for(curdef = target->defbase.first; curdef;
-                   curdef = curdef->next, index++)
-                       if(!strcmp(curdef->name, vgroup)) break;
+               int index = defgroup_name_index(target, vgroup);
+               float weight;
 
-               if(curdef && (me->dvert || dm)) {
+               if(index >= 0 && (me->dvert || dm)) {
                        MDeformVert *dvert = me->dvert;
-                       int j;
                        
                        for(a = 0; a < numVerts; a++, dvert++) {
                                if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
-                               for(j = 0; j < dvert->totweight; j++) {
-                                       if (dvert->dw[j].def_nr == index) {
-                                               calc_latt_deform(laOb, vertexCos[a], dvert->dw[j].weight);
-                                       }
-                               }
+
+                               weight= defvert_find_weight(dvert, index);
+
+                               if(weight > 0.0f)
+                                       calc_latt_deform(laOb, vertexCos[a], weight);
                        }
                }
        } else {
@@ -867,14 +868,14 @@ void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
        end_latt_deform(laOb);
 }
 
-int object_deform_mball(Object *ob)
+int object_deform_mball(Object *ob, ListBase *dispbase)
 {
        if(ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) {
                DispList *dl;
 
-               for (dl=ob->disp.first; dl; dl=dl->next) {
+               for (dl=dispbase->first; dl; dl=dl->next) {
                        lattice_deform_verts(ob->parent, ob, NULL,
-                                            (float(*)[3]) dl->verts, dl->nr, NULL);
+                                                                (float(*)[3]) dl->verts, dl->nr, NULL);
                }
 
                return 1;
@@ -964,13 +965,13 @@ float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3]
        int i, numVerts;
        float (*vertexCos)[3];
 
-       if(lt->editlatt) lt= lt->editlatt;
+       if(lt->editlatt) lt= lt->editlatt->latt;
        numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
        
        vertexCos = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
        
        for (i=0; i<numVerts; i++) {
-               VECCOPY(vertexCos[i], lt->def[i].vec);
+               copy_v3_v3(vertexCos[i], lt->def[i].vec);
        }
 
        return vertexCos;
@@ -982,7 +983,7 @@ void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
        int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
 
        for (i=0; i<numVerts; i++) {
-               VECCOPY(lt->def[i].vec, vertexCos[i]);
+               copy_v3_v3(lt->def[i].vec, vertexCos[i]);
        }
 }
 
@@ -1028,7 +1029,7 @@ struct MDeformVert* lattice_get_deform_verts(struct Object *oblatt)
        if(oblatt->type == OB_LATTICE)
        {
                Lattice *lt = (Lattice*)oblatt->data;
-               if(lt->editlatt) lt= lt->editlatt;
+               if(lt->editlatt) lt= lt->editlatt->latt;
                return lt->dvert;
        }