merge with trunk at r31523
[blender.git] / source / blender / editors / transform / transform_snap.c
index ae28986017fad0adbf0e8c2e53ecfd7e6125064d..3dbd83bfa52b4913639efcc1c7eca8bbe441734e 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * $Id
+ * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
@@ -15,7 +15,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 "PIL_time.h"
 
-#include "DNA_action_types.h"
 #include "DNA_armature_types.h"
-#include "DNA_object_types.h"
 #include "DNA_scene_types.h"
+#include "DNA_object_types.h"
 #include "DNA_meshdata_types.h" // Temporary, for snapping to other unselected meshes
 #include "DNA_space_types.h"
 #include "DNA_screen_types.h"
-#include "DNA_userdef_types.h"
 #include "DNA_view3d_types.h"
 #include "DNA_windowmanager_types.h"
 
 //#include "editmesh.h"
 //#include "BIF_editsima.h"
 #include "BIF_gl.h"
-#include "BIF_glutil.h"
 //#include "BIF_mywindow.h"
 //#include "BIF_screen.h"
 //#include "BIF_editsima.h"
 //#include "BIF_drawimage.h"
 //#include "BIF_editmesh.h"
 
-#include "BKE_global.h"
-#include "BKE_utildefines.h"
 #include "BKE_DerivedMesh.h"
 #include "BKE_object.h"
 #include "BKE_anim.h" /* for duplis */
 #include "BKE_context.h"
+#include "BKE_tessmesh.h"
+#include "BKE_mesh.h"
 
 #include "ED_armature.h"
 #include "ED_image.h"
 #include "ED_mesh.h"
-#include "ED_transform.h"
 #include "ED_uvedit.h"
 #include "ED_view3d.h"
 
@@ -90,7 +86,7 @@
 
 /********************* PROTOTYPES ***********************/
 
-void setSnappingCallback(TransInfo *t, short snap_target);
+void setSnappingCallback(TransInfo *t);
 
 void ApplySnapTranslation(TransInfo *t, float vec[3]);
 void ApplySnapRotation(TransInfo *t, float *vec);
@@ -123,15 +119,20 @@ int BIF_snappingSupported(Object *obedit)
        return status;
 }
 
-int validSnap(TransInfo *t) {
+int validSnap(TransInfo *t)
+{
        return (t->tsnap.status & (POINT_INIT|TARGET_INIT)) == (POINT_INIT|TARGET_INIT) ||
                        (t->tsnap.status & (MULTI_POINTS|TARGET_INIT)) == (MULTI_POINTS|TARGET_INIT);
 }
 
+int activeSnap(TransInfo *t)
+{
+       return (t->modifiers & (MOD_SNAP|MOD_SNAP_INVERT)) == MOD_SNAP || (t->modifiers & (MOD_SNAP|MOD_SNAP_INVERT)) == MOD_SNAP_INVERT;
+}
+
 void drawSnapping(const struct bContext *C, TransInfo *t)
 {
-       if (validSnap(t) &&
-               (t->modifiers & MOD_SNAP))
+       if (validSnap(t) && activeSnap(t))
                {
                
                char col[4] = {1, 0, 1};
@@ -232,7 +233,7 @@ int  handleSnapping(TransInfo *t, wmEvent *event)
 void applyProject(TransInfo *t)
 {
        /* XXX FLICKER IN OBJECT MODE */
-       if ((t->tsnap.project) && (t->modifiers & MOD_SNAP) && (t->modifiers & MOD_SNAP))
+       if ((t->tsnap.project) && activeSnap(t) && (t->flag & T_NO_PROJECT) == 0)
        {
                TransData *td = t->data;
                float tvec[3];
@@ -268,7 +269,7 @@ void applyProject(TransInfo *t)
                        
                        project_float(t->ar, iloc, mval);
                        
-                       if (snapObjectsTransform(t, mval, &dist, loc, no, t->tsnap.modeTarget))
+                       if (snapObjectsTransform(t, mval, &dist, loc, no, t->tsnap.modeSelect))
                        {
 //                             if(t->flag & (T_EDIT|T_POSE)) {
 //                                     mul_m4_v3(imat, loc);
@@ -278,7 +279,7 @@ void applyProject(TransInfo *t)
                                
                                mul_m3_v3(td->smtx, tvec);
                                
-                               add_v3_v3v3(td->loc, td->loc, tvec);
+                               add_v3_v3(td->loc, tvec);
                        }
                        
                        //XXX constraintTransLim(t, td);
@@ -298,8 +299,7 @@ void applySnapping(TransInfo *t, float *vec)
        
                t->tsnap.applySnap(t, vec);
        }
-       else if ((t->tsnap.mode != SCE_SNAP_MODE_INCREMENT) &&
-               (t->modifiers & MOD_SNAP))
+       else if ((t->tsnap.mode != SCE_SNAP_MODE_INCREMENT) && activeSnap(t))
        {
                double current = PIL_check_seconds_timer();
                
@@ -323,9 +323,10 @@ void resetSnapping(TransInfo *t)
 {
        t->tsnap.status = 0;
        t->tsnap.align = 0;
+       t->tsnap.project = 0;
        t->tsnap.mode = 0;
        t->tsnap.modeSelect = 0;
-       t->tsnap.modeTarget = 0;
+       t->tsnap.target = 0;
        t->tsnap.last = 0;
        t->tsnap.applySnap = NULL;
 
@@ -352,11 +353,65 @@ int validSnappingNormal(TransInfo *t)
        return 0;
 }
 
-void initSnapping(TransInfo *t, wmOperator *op)
+void initSnappingMode(TransInfo *t)
 {
        ToolSettings *ts = t->settings;
        Object *obedit = t->obedit;
        Scene *scene = t->scene;
+
+       /* force project off when not supported */
+       if (ts->snap_mode != SCE_SNAP_MODE_FACE)
+       {
+               t->tsnap.project = 0;
+       }
+
+       t->tsnap.mode = ts->snap_mode;
+
+       if ((t->spacetype == SPACE_VIEW3D || t->spacetype == SPACE_IMAGE) && // Only 3D view or UV
+                       (t->flag & T_CAMERA) == 0) { // Not with camera selected in camera view
+               setSnappingCallback(t);
+
+               /* Edit mode */
+               if (t->tsnap.applySnap != NULL && // A snapping function actually exist
+                       (obedit != NULL && ELEM3(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE)) ) // Temporary limited to edit mode meshes, armature, curves
+               {
+                       if ((t->flag & T_PROP_EDIT) || t->tsnap.project) /* also exclude edit for project, for now */
+                       {
+                               t->tsnap.modeSelect = SNAP_NOT_OBEDIT;
+                       }
+                       else
+                       {
+                               t->tsnap.modeSelect = SNAP_ALL;
+                       }
+               }
+               /* Particles edit mode*/
+               else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
+                       (obedit == NULL && BASACT && BASACT->object && BASACT->object->mode & OB_MODE_PARTICLE_EDIT ))
+               {
+                       t->tsnap.modeSelect = SNAP_ALL;
+               }
+               /* Object mode */
+               else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
+                       (obedit == NULL) ) // Object Mode
+               {
+                       t->tsnap.modeSelect = SNAP_NOT_SELECTED;
+               }
+               else
+               {
+                       /* Grid if snap is not possible */
+                       t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
+               }
+       }
+       else
+       {
+               /* Always grid outside of 3D view */
+               t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
+       }
+}
+
+void initSnapping(TransInfo *t, wmOperator *op)
+{
+       ToolSettings *ts = t->settings;
        short snap_target = t->settings->snap_target;
        
        resetSnapping(t);
@@ -387,9 +442,9 @@ void initSnapping(TransInfo *t, wmOperator *op)
                                normalize_v3(t->tsnap.snapNormal);
                        }
 
-                       if (RNA_struct_find_property(op->ptr, "snap_project"))
+                       if (RNA_struct_find_property(op->ptr, "use_snap_project"))
                        {
-                               t->tsnap.project = RNA_boolean_get(op->ptr, "snap_project");
+                               t->tsnap.project = RNA_boolean_get(op->ptr, "use_snap_project");
                        }
                }
        }
@@ -405,76 +460,27 @@ void initSnapping(TransInfo *t, wmOperator *op)
                t->tsnap.peel = ((t->settings->snap_flag & SCE_SNAP_PROJECT) == SCE_SNAP_PROJECT);
        }
        
-       /* force project off when not supported */
-       if (ts->snap_mode != SCE_SNAP_MODE_FACE)
-       {
-               t->tsnap.project = 0;
-       }
-       
-       t->tsnap.mode = ts->snap_mode;
+       t->tsnap.target = snap_target;
 
-       if ((t->spacetype == SPACE_VIEW3D || t->spacetype == SPACE_IMAGE) && // Only 3D view or UV
-                       (t->flag & T_CAMERA) == 0) { // Not with camera selected in camera view
-               setSnappingCallback(t, snap_target);
-
-               /* Edit mode */
-               if (t->tsnap.applySnap != NULL && // A snapping function actually exist
-                       (obedit != NULL && ELEM3(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE)) ) // Temporary limited to edit mode meshes, armature, curves
-               {
-                       if (t->flag & T_PROP_EDIT)
-                       {
-                               t->tsnap.modeSelect = SNAP_NOT_OBEDIT;
-                       }
-                       else
-                       {
-                               t->tsnap.modeSelect = SNAP_ALL;
-                       }
-               }
-               /* Particles edit mode*/
-               else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
-                       (obedit == NULL && BASACT && BASACT->object && BASACT->object->mode & OB_MODE_PARTICLE_EDIT ))
-               {
-                       t->tsnap.modeSelect = SNAP_ALL;
-               }
-               /* Object mode */
-               else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
-                       (obedit == NULL) ) // Object Mode
-               {
-                       t->tsnap.modeSelect = SNAP_NOT_SELECTED;
-               }
-               else
-               {       
-                       /* Grid if snap is not possible */
-                       t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
-               }
-       }
-       else
-       {
-               /* Always grid outside of 3D view */
-               t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
-       }
+       initSnappingMode(t);
 }
 
-void setSnappingCallback(TransInfo *t, short snap_target)
+void setSnappingCallback(TransInfo *t)
 {
        t->tsnap.calcSnap = CalcSnapGeometry;
 
-       switch(snap_target)
+       switch(t->tsnap.target)
        {
                case SCE_SNAP_TARGET_CLOSEST:
-                       t->tsnap.modeTarget = SNAP_CLOSEST;
                        t->tsnap.targetSnap = TargetSnapClosest;
                        break;
                case SCE_SNAP_TARGET_CENTER:
-                       t->tsnap.modeTarget = SNAP_CENTER;
                        t->tsnap.targetSnap = TargetSnapCenter;
                        break;
                case SCE_SNAP_TARGET_MEDIAN:
-                       t->tsnap.modeTarget = SNAP_MEDIAN;
                        t->tsnap.targetSnap = TargetSnapMedian;
                        break;
                case SCE_SNAP_TARGET_ACTIVE:
-                       t->tsnap.modeTarget = SNAP_ACTIVE;
                        t->tsnap.targetSnap = TargetSnapActive;
                        break;
 
@@ -491,8 +497,8 @@ void setSnappingCallback(TransInfo *t, short snap_target)
                t->tsnap.distance = RotationBetween;
                
                // Can't do TARGET_CENTER with rotation, use TARGET_MEDIAN instead
-               if (snap_target == SCE_SNAP_TARGET_CENTER) {
-                       t->tsnap.modeTarget = SNAP_MEDIAN;
+               if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
+                       t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
                        t->tsnap.targetSnap = TargetSnapMedian;
                }
                break;
@@ -501,8 +507,8 @@ void setSnappingCallback(TransInfo *t, short snap_target)
                t->tsnap.distance = ResizeBetween;
                
                // Can't do TARGET_CENTER with resize, use TARGET_MEDIAN instead
-               if (snap_target == SCE_SNAP_TARGET_CENTER) {
-                       t->tsnap.modeTarget = SNAP_MEDIAN;
+               if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
+                       t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
                        t->tsnap.targetSnap = TargetSnapMedian;
                }
                break;
@@ -569,7 +575,7 @@ void ApplySnapTranslation(TransInfo *t, float vec[3])
 
 void ApplySnapRotation(TransInfo *t, float *vec)
 {
-       if (t->tsnap.modeTarget == SNAP_CLOSEST) {
+       if (t->tsnap.target == SCE_SNAP_TARGET_CLOSEST) {
                *vec = t->tsnap.dist;
        }
        else {
@@ -581,7 +587,7 @@ void ApplySnapRotation(TransInfo *t, float *vec)
 
 void ApplySnapResize(TransInfo *t, float vec[3])
 {
-       if (t->tsnap.modeTarget == SNAP_CLOSEST) {
+       if (t->tsnap.target == SCE_SNAP_TARGET_CLOSEST) {
                vec[0] = vec[1] = vec[2] = t->tsnap.dist;
        }
        else {
@@ -789,8 +795,8 @@ void CalcSnapGeometry(TransInfo *t, float *vec)
                else
                {
                        found = snapObjectsTransform(t, mval, &dist, loc, no, t->tsnap.modeSelect);
-               }
-               
+                       }
+                               
                if (found == 1)
                {
                        float tangent[3];
@@ -827,8 +833,6 @@ void CalcSnapGeometry(TransInfo *t, float *vec)
                        t->tsnap.snapPoint[0] *= aspx;
                        t->tsnap.snapPoint[1] *= aspy;
 
-                       mul_m4_v3(t->obedit->obmat, t->tsnap.snapPoint);
-                       
                        t->tsnap.status |=  POINT_INIT;
                }
                else
@@ -887,7 +891,7 @@ void TargetSnapActive(TransInfo *t)
                /* No active, default to median */
                else
                {
-                       t->tsnap.modeTarget = SNAP_MEDIAN;
+                       t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
                        t->tsnap.targetSnap = TargetSnapMedian;
                        TargetSnapMedian(t);
                }               
@@ -908,7 +912,7 @@ void TargetSnapMedian(TransInfo *t)
                
                for(td = t->data, i = 0 ; i < t->total && td->flag & TD_SELECTED ; i++, td++)
                {
-                       add_v3_v3v3(t->tsnap.snapTarget, t->tsnap.snapTarget, td->center);
+                       add_v3_v3(t->tsnap.snapTarget, td->center);
                }
                
                mul_v3_fl(t->tsnap.snapTarget, 1.0 / i);
@@ -1026,7 +1030,7 @@ int snapFace(ARegion *ar, float v1co[3], float v2co[3], float v3co[3], float *v4
                
                VECCOPY(intersect, ray_normal_local);
                mul_v3_fl(intersect, lambda);
-               add_v3_v3v3(intersect, intersect, ray_start_local);
+               add_v3_v3(intersect, ray_start_local);
                
                VECCOPY(location, intersect);
                
@@ -1254,11 +1258,11 @@ int snapArmature(short snap_mode, ARegion *ar, Object *ob, bArmature *arm, float
        return retval;
 }
 
-int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, EditMesh *em, float obmat[][4], float ray_start[3], float ray_normal[3], float mval[2], float *loc, float *no, int *dist, float *depth)
+int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, BMEditMesh *em, float obmat[][4], float ray_start[3], float ray_normal[3], short mval[2], float *loc, float *no, int *dist, float *depth)
 {
        int retval = 0;
        int totvert = dm->getNumVerts(dm);
-       int totface = dm->getNumFaces(dm);
+       int totface = dm->getNumTessFaces(dm);
        
        if (totvert > 0) {
                float imat[4][4];
@@ -1322,19 +1326,19 @@ int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, E
 
 #else
                                        MVert *verts = dm->getVertArray(dm);
-                                       MFace *faces = dm->getFaceArray(dm);
+                                       MFace *faces = dm->getTessFaceArray(dm);
                                        int *index_array = NULL;
                                        int index = 0;
                                        int i;
                                        
                                        if (em != NULL)
                                        {
-                                               index_array = dm->getFaceDataArray(dm, CD_ORIGINDEX);
-                                               EM_init_index_arrays(em, 0, 0, 1);
+                                               index_array = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
+                                               EDBM_init_index_arrays(em, 0, 0, 1);
                                        }
                                        
                                        for( i = 0; i < totface; i++) {
-                                               EditFace *efa = NULL;
+                                               BMFace *efa = NULL;
                                                MFace *f = faces + i;
                                                
                                                test = 1; /* reset for every face */
@@ -1356,11 +1360,22 @@ int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, E
                                                        }
                                                        else
                                                        {
-                                                               efa = EM_get_face_for_index(index);
+                                                               efa = EDBM_get_face_for_index(em, index);
                                                                
-                                                               if (efa && (efa->h || (efa->v1->f & SELECT) || (efa->v2->f & SELECT) || (efa->v3->f & SELECT) || (efa->v4 && efa->v4->f & SELECT)))
+                                                               if (efa && BM_TestHFlag(efa, BM_HIDDEN))
                                                                {
                                                                        test = 0;
+                                                               } else if (efa) {
+                                                                       BMIter iter;
+                                                                       BMLoop *l;
+                                                                       
+                                                                       l = BMIter_New(&iter, em->bm, BM_LOOPS_OF_FACE, efa);
+                                                                       for ( ; l; l=BMIter_Step(&iter)) {
+                                                                               if (BM_TestHFlag(l->v, BM_SELECT)) {
+                                                                                       test = 0;
+                                                                                       break;
+                                                                               }
+                                                                       }
                                                                }
                                                        }
                                                }
@@ -1388,7 +1403,7 @@ int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, E
                                        
                                        if (em != NULL)
                                        {
-                                               EM_free_index_arrays();
+                                               EDBM_free_index_arrays(em);
                                        }
 #endif
                                        break;
@@ -1403,11 +1418,11 @@ int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, E
                                        if (em != NULL)
                                        {
                                                index_array = dm->getVertDataArray(dm, CD_ORIGINDEX);
-                                               EM_init_index_arrays(em, 1, 0, 0);
+                                               EDBM_init_index_arrays(em, 1, 0, 0);
                                        }
                                        
                                        for( i = 0; i < totvert; i++) {
-                                               EditVert *eve = NULL;
+                                               BMVert *eve = NULL;
                                                MVert *v = verts + i;
                                                
                                                test = 1; /* reset for every vert */
@@ -1429,9 +1444,9 @@ int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, E
                                                        }
                                                        else
                                                        {
-                                                               eve = EM_get_vert_for_index(index);
+                                                               eve = EDBM_get_vert_for_index(em, index);
                                                                
-                                                               if (eve && (eve->h || (eve->f & SELECT)))
+                                                               if (eve && (BM_TestHFlag(eve, BM_HIDDEN) || BM_TestHFlag(eve, BM_SELECT)))
                                                                {
                                                                        test = 0;
                                                                }
@@ -1447,7 +1462,7 @@ int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, E
 
                                        if (em != NULL)
                                        {
-                                               EM_free_index_arrays();
+                                               EDBM_free_index_arrays(em);
                                        }
                                        break;
                                }
@@ -1463,11 +1478,11 @@ int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, E
                                        if (em != NULL)
                                        {
                                                index_array = dm->getEdgeDataArray(dm, CD_ORIGINDEX);
-                                               EM_init_index_arrays(em, 0, 1, 0);
+                                               EDBM_init_index_arrays(em, 0, 1, 0);
                                        }
                                        
                                        for( i = 0; i < totedge; i++) {
-                                               EditEdge *eed = NULL;
+                                               BMEdge *eed = NULL;
                                                MEdge *e = edges + i;
                                                
                                                test = 1; /* reset for every vert */
@@ -1489,9 +1504,11 @@ int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, E
                                                        }
                                                        else
                                                        {
-                                                               eed = EM_get_edge_for_index(index);
+                                                               eed = EDBM_get_edge_for_index(em, index);
                                                                
-                                                               if (eed && (eed->h || (eed->v1->f & SELECT) || (eed->v2->f & SELECT)))
+                                                               if (eed && (BM_TestHFlag(eed, BM_HIDDEN) ||
+                                                                       BM_TestHFlag(eed->v1, BM_SELECT) || 
+                                                                       BM_TestHFlag(eed->v2, BM_SELECT)))
                                                                {
                                                                        test = 0;
                                                                }
@@ -1507,7 +1524,7 @@ int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, E
 
                                        if (em != NULL)
                                        {
-                                               EM_free_index_arrays();
+                                               EDBM_free_index_arrays(em);
                                        }
                                        break;
                                }
@@ -1524,13 +1541,13 @@ int snapObject(Scene *scene, ARegion *ar, Object *ob, int editobject, float obma
        int retval = 0;
        
        if (ob->type == OB_MESH) {
-               EditMesh *em;
+               BMEditMesh *em;
                DerivedMesh *dm;
                
                if (editobject)
                {
-                       em = ((Mesh *)ob->data)->edit_mesh;
-                       dm = editmesh_get_derived_cage(scene, ob, em, CD_MASK_BAREMESH);
+                       em = ((Mesh *)ob->data)->edit_btmesh;
+                       dm = editbmesh_get_derived_cage(scene, ob, em, CD_MASK_BAREMESH);
                }
                else
                {
@@ -1570,9 +1587,10 @@ int snapObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, float mv
         *
         * To solve that problem, we do it first as an exception. 
         * */
-       if(BASACT->object && BASACT->object->mode & OB_MODE_PARTICLE_EDIT)
+       base= BASACT;
+       if(base && base->object && base->object->mode & OB_MODE_PARTICLE_EDIT)
        {
-               Object *ob = BASACT->object;
+               Object *ob = base->object;
                retval |= snapObject(scene, ar, ob, 0, ob->obmat, ray_start, ray_normal, mval, loc, no, dist, &depth);
        }
        
@@ -1677,7 +1695,7 @@ int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4], float ray_sta
 {
        int retval = 0;
        int totvert = dm->getNumVerts(dm);
-       int totface = dm->getNumFaces(dm);
+       int totface = dm->getNumTessFaces(dm);
        
        if (totvert > 0) {
                float imat[4][4];
@@ -1707,7 +1725,7 @@ int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4], float ray_sta
                
                if (test == 1) {
                        MVert *verts = dm->getVertArray(dm);
-                       MFace *faces = dm->getFaceArray(dm);
+                       MFace *faces = dm->getTessFaceArray(dm);
                        int i;
                        
                        for( i = 0; i < totface; i++) {
@@ -1725,7 +1743,7 @@ int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4], float ray_sta
                                        
                                        VECCOPY(intersect, ray_normal_local);
                                        mul_v3_fl(intersect, lambda);
-                                       add_v3_v3v3(intersect, intersect, ray_start_local);
+                                       add_v3_v3(intersect, ray_start_local);
                                        
                                        VECCOPY(location, intersect);
                                        
@@ -1755,7 +1773,7 @@ int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4], float ray_sta
                                                
                                                VECCOPY(intersect, ray_normal_local);
                                                mul_v3_fl(intersect, lambda);
-                                               add_v3_v3v3(intersect, intersect, ray_start_local);
+                                               add_v3_v3(intersect, ray_start_local);
                                                
                                                VECCOPY(location, intersect);
                                                
@@ -1803,6 +1821,7 @@ int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, ListBase
                                        Object *ob = dupli_ob->ob;
                                        
                                        if (ob->type == OB_MESH) {
+#if 0 //BMESH_TODO
                                                EditMesh *em;
                                                DerivedMesh *dm = NULL;
                                                int val;
@@ -1824,6 +1843,7 @@ int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, ListBase
                                                retval = retval || val;
                                                
                                                dm->release(dm);
+#endif
                                        }
                                }
                                
@@ -1831,7 +1851,7 @@ int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, ListBase
                        }
                        
                        if (ob->type == OB_MESH) {
-                               EditMesh *em;
+                               BMEditMesh *em;
                                DerivedMesh *dm = NULL;
                                int val;
 
@@ -1843,8 +1863,8 @@ int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, ListBase
                                }
                                else
                                {
-                                       em = ((Mesh *)ob->data)->edit_mesh;
-                                       dm = editmesh_get_derived_cage(scene, obedit, em, CD_MASK_BAREMESH);
+                                       em = ((Mesh *)ob->data)->edit_btmesh;
+                                       dm = editbmesh_get_derived_cage(scene, obedit, em, CD_MASK_BAREMESH);
                                        
                                        val = peelDerivedMesh(ob, dm, ob->obmat, ray_start, ray_normal, mval, depth_peels);
                                }
@@ -1892,26 +1912,13 @@ void snapGridAction(TransInfo *t, float *val, GearsType action) {
 
 
 void snapGrid(TransInfo *t, float *val) {
-       int invert;
        GearsType action;
 
        // Only do something if using Snap to Grid
        if (t->tsnap.mode != SCE_SNAP_MODE_INCREMENT)
                return;
 
-       if(t->mode==TFM_ROTATION || t->mode==TFM_WARP || t->mode==TFM_TILT || t->mode==TFM_TRACKBALL || t->mode==TFM_BONE_ROLL)
-               invert = U.flag & USER_AUTOROTGRID;
-       else if(t->mode==TFM_RESIZE || t->mode==TFM_SHEAR || t->mode==TFM_BONESIZE || t->mode==TFM_SHRINKFATTEN || t->mode==TFM_CURVE_SHRINKFATTEN)
-               invert = U.flag & USER_AUTOSIZEGRID;
-       else
-               invert = U.flag & USER_AUTOGRABGRID;
-
-       if(invert) {
-               action = (t->modifiers & MOD_SNAP) ? NO_GEARS: BIG_GEARS;
-       }
-       else {
-               action = (t->modifiers & MOD_SNAP) ? BIG_GEARS : NO_GEARS;
-       }
+       action = activeSnap(t) ? BIG_GEARS : NO_GEARS;
 
        if (action == BIG_GEARS && (t->modifiers & MOD_PRECISION)) {
                action = SMALL_GEARS;