code cleanup: function naming, use BKE_*type* prefix.
[blender.git] / source / blender / editors / transform / transform_snap.c
index 4514b36f3a521444c7e3d653646e69434371caf0..b875886e80a09ee8bc465d893f4c47fe10508129 100644 (file)
@@ -40,6 +40,7 @@
 #include "DNA_armature_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_object_types.h"
+#include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h" // Temporary, for snapping to other unselected meshes
 #include "DNA_space_types.h"
 #include "DNA_screen_types.h"
@@ -49,7 +50,6 @@
 #include "RNA_access.h"
 
 #include "BLI_math.h"
-#include "BLI_editVert.h"
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
 
@@ -140,8 +140,7 @@ int activeSnap(TransInfo *t)
 
 void drawSnapping(const struct bContext *C, TransInfo *t)
 {
-       if (validSnap(t) && activeSnap(t))
-               {
+       if (validSnap(t) && activeSnap(t)) {
                
                unsigned char col[4], selectedCol[4], activeCol[4];
                UI_GetThemeColor3ubv(TH_TRANSFORM, col);
@@ -169,7 +168,8 @@ void drawSnapping(const struct bContext *C, TransInfo *t)
                        for (p = t->tsnap.points.first; p; p = p->next) {
                                if (p == t->tsnap.selectedPoint) {
                                        glColor4ubv(selectedCol);
-                               } else {
+                               }
+                               else {
                                        glColor4ubv(col);
                                }
 
@@ -183,26 +183,23 @@ void drawSnapping(const struct bContext *C, TransInfo *t)
                        }
                        
                        /* draw normal if needed */
-                       if (usingSnappingNormal(t) && validSnappingNormal(t))
-                       {
+                       if (usingSnappingNormal(t) && validSnappingNormal(t)) {
                                glColor4ubv(activeCol);
 
                                glBegin(GL_LINES);
                                        glVertex3f(t->tsnap.snapPoint[0], t->tsnap.snapPoint[1], t->tsnap.snapPoint[2]);
-                                       glVertex3f(     t->tsnap.snapPoint[0] + t->tsnap.snapNormal[0],
-                                                               t->tsnap.snapPoint[1] + t->tsnap.snapNormal[1],
-                                                               t->tsnap.snapPoint[2] + t->tsnap.snapNormal[2]);
-                               glEnd();
+                                       glVertex3f(t->tsnap.snapPoint[0] + t->tsnap.snapNormal[0],
+                                                  t->tsnap.snapPoint[1] + t->tsnap.snapNormal[1],
+                                                  t->tsnap.snapPoint[2] + t->tsnap.snapNormal[2]);
+                                       glEnd();
                        }
                        
-                       if(v3d->zbuf)
+                       if (v3d->zbuf)
                                glEnable(GL_DEPTH_TEST);
                }
-               else if (t->spacetype==SPACE_IMAGE)
-               {
-                       /*This will not draw, and Im nor sure why - campbell */
-                       
-                       /*                      
+               else if (t->spacetype==SPACE_IMAGE) {
+                       /* This will not draw, and Im nor sure why - campbell */
+#if 0
                        float xuser_asp, yuser_asp;
                        int wi, hi;
                        float w, h;
@@ -219,7 +216,7 @@ void drawSnapping(const struct bContext *C, TransInfo *t)
                        cpack(0xFFFFFF);
                        glTranslatef(t->tsnap.snapPoint[0], t->tsnap.snapPoint[1], 0.0f);
                        
-                       //glRectf(0,0,1,1);
+                       //glRectf(0, 0, 1, 1);
                        
                        setlinestyle(0);
                        cpack(0x0);
@@ -230,7 +227,7 @@ void drawSnapping(const struct bContext *C, TransInfo *t)
                        
                        glTranslatef(-t->tsnap.snapPoint[0], -t->tsnap.snapPoint[1], 0.0f);
                        setlinestyle(0);
-                       */
+#endif
                        
                }
        }
@@ -241,16 +238,14 @@ int  handleSnapping(TransInfo *t, wmEvent *event)
        int status = 0;
 
 #if 0 // XXX need a proper selector for all snap mode
-       if (BIF_snappingSupported(t->obedit) && event->type == TABKEY && event->shift)
-       {
+       if (BIF_snappingSupported(t->obedit) && event->type == TABKEY && event->shift) {
                /* toggle snap and reinit */
                t->settings->snap_flag ^= SCE_SNAP;
                initSnapping(t, NULL);
                status = 1;
        }
 #endif
-       if (event->type == MOUSEMOVE)
-       {
+       if (event->type == MOUSEMOVE) {
                status |= updateSelectedSnapPoint(t);
        }
        
@@ -260,19 +255,18 @@ int  handleSnapping(TransInfo *t, wmEvent *event)
 void applyProject(TransInfo *t)
 {
        /* XXX FLICKER IN OBJECT MODE */
-       if ((t->tsnap.project) && activeSnap(t) && (t->flag & T_NO_PROJECT) == 0)
-       {
+       if ((t->tsnap.project) && activeSnap(t) && (t->flag & T_NO_PROJECT) == 0) {
                TransData *td = t->data;
                float tvec[3];
                float imat[4][4];
                int i;
        
-               if(t->flag & (T_EDIT|T_POSE)) {
+               if (t->flag & (T_EDIT|T_POSE)) {
                        Object *ob = t->obedit?t->obedit:t->poseobj;
                        invert_m4_m4(imat, ob->obmat);
                }
 
-               for(i = 0 ; i < t->total; i++, td++) {
+               for (i = 0 ; i < t->total; i++, td++) {
                        float iloc[3], loc[3], no[3];
                        float mval[2];
                        int dist = 1000;
@@ -284,23 +278,20 @@ void applyProject(TransInfo *t)
                                continue;
                        
                        copy_v3_v3(iloc, td->loc);
-                       if (t->flag & (T_EDIT|T_POSE))
-                       {
+                       if (t->flag & (T_EDIT|T_POSE)) {
                                Object *ob = t->obedit?t->obedit:t->poseobj;
                                mul_m4_v3(ob->obmat, iloc);
                        }
-                       else if (t->flag & T_OBJECT)
-                       {
+                       else if (t->flag & T_OBJECT) {
                                td->ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
-                               object_handle_update(t->scene, td->ob);
+                               BKE_object_handle_update(t->scene, td->ob);
                                copy_v3_v3(iloc, td->ob->obmat[3]);
                        }
                        
                        project_float(t->ar, iloc, mval);
                        
-                       if (snapObjectsTransform(t, mval, &dist, loc, no, t->tsnap.modeSelect))
-                       {
-//                             if(t->flag & (T_EDIT|T_POSE)) {
+                       if (snapObjectsTransform(t, mval, &dist, loc, no, t->tsnap.modeSelect)) {
+//                             if (t->flag & (T_EDIT|T_POSE)) {
 //                                     mul_m4_v3(imat, loc);
 //                             }
 //                             
@@ -322,27 +313,23 @@ void applySnapping(TransInfo *t, float *vec)
        if (t->tsnap.project)
                return;
        
-       if (t->tsnap.status & SNAP_FORCED)
-       {
+       if (t->tsnap.status & SNAP_FORCED) {
                t->tsnap.targetSnap(t);
        
                t->tsnap.applySnap(t, vec);
        }
-       else if ((t->tsnap.mode != SCE_SNAP_MODE_INCREMENT) && activeSnap(t))
-       {
+       else if ((t->tsnap.mode != SCE_SNAP_MODE_INCREMENT) && activeSnap(t)) {
                double current = PIL_check_seconds_timer();
                
                // Time base quirky code to go around findnearest slowness
                /* !TODO! add exception for object mode, no need to slow it down then */
-               if (current - t->tsnap.last  >= 0.01)
-               {
+               if (current - t->tsnap.last  >= 0.01) {
                        t->tsnap.calcSnap(t, vec);
                        t->tsnap.targetSnap(t);
        
                        t->tsnap.last = current;
                }
-               if (validSnap(t))
-               {
+               if (validSnap(t)) {
                        t->tsnap.applySnap(t, vec);
                }
        }
@@ -371,10 +358,8 @@ int usingSnappingNormal(TransInfo *t)
 
 int validSnappingNormal(TransInfo *t)
 {
-       if (validSnap(t))
-       {
-               if (dot_v3v3(t->tsnap.snapNormal, t->tsnap.snapNormal) > 0)
-               {
+       if (validSnap(t)) {
+               if (dot_v3v3(t->tsnap.snapNormal, t->tsnap.snapNormal) > 0) {
                        return 1;
                }
        }
@@ -389,8 +374,7 @@ static void initSnappingMode(TransInfo *t)
        Scene *scene = t->scene;
 
        /* force project off when not supported */
-       if (ts->snap_mode != SCE_SNAP_MODE_FACE)
-       {
+       if (ts->snap_mode != SCE_SNAP_MODE_FACE) {
                t->tsnap.project = 0;
        }
 
@@ -405,12 +389,10 @@ static void initSnappingMode(TransInfo *t)
                        (obedit != NULL && ELEM4(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE)) ) // Temporary limited to edit mode meshes, armature, curves
                {
                        /* Exclude editmesh if using proportional edit */
-                       if ((obedit->type == OB_MESH) && (t->flag & T_PROP_EDIT))
-                       {
+                       if ((obedit->type == OB_MESH) && (t->flag & T_PROP_EDIT)) {
                                t->tsnap.modeSelect = SNAP_NOT_OBEDIT;
                        }
-                       else
-                       {
+                       else {
                                t->tsnap.modeSelect = t->tsnap.snap_self ? SNAP_ALL : SNAP_NOT_OBEDIT;
                        }
                }
@@ -426,14 +408,12 @@ static void initSnappingMode(TransInfo *t)
                {
                        t->tsnap.modeSelect = SNAP_NOT_SELECTED;
                }
-               else
-               {
+               else {
                        /* Grid if snap is not possible */
                        t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
                }
        }
-       else
-       {
+       else {
                /* Always grid outside of 3D view */
                t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
        }
@@ -447,47 +427,38 @@ void initSnapping(TransInfo *t, wmOperator *op)
        resetSnapping(t);
        
        /* if snap property exists */
-       if (op && RNA_struct_find_property(op->ptr, "snap") && RNA_property_is_set(op->ptr, "snap"))
-       {
-               if (RNA_boolean_get(op->ptr, "snap"))
-               {
+       if (op && RNA_struct_find_property(op->ptr, "snap") && RNA_struct_property_is_set(op->ptr, "snap")) {
+               if (RNA_boolean_get(op->ptr, "snap")) {
                        t->modifiers |= MOD_SNAP;
 
-                       if (RNA_property_is_set(op->ptr, "snap_target"))
-                       {
+                       if (RNA_struct_property_is_set(op->ptr, "snap_target")) {
                                snap_target = RNA_enum_get(op->ptr, "snap_target");
                        }
                        
-                       if (RNA_property_is_set(op->ptr, "snap_point"))
-                       {
+                       if (RNA_struct_property_is_set(op->ptr, "snap_point")) {
                                RNA_float_get_array(op->ptr, "snap_point", t->tsnap.snapPoint);
                                t->tsnap.status |= SNAP_FORCED|POINT_INIT;
                        }
                        
                        /* snap align only defined in specific cases */
-                       if (RNA_struct_find_property(op->ptr, "snap_align"))
-                       {
+                       if (RNA_struct_find_property(op->ptr, "snap_align")) {
                                t->tsnap.align = RNA_boolean_get(op->ptr, "snap_align");
                                RNA_float_get_array(op->ptr, "snap_normal", t->tsnap.snapNormal);
                                normalize_v3(t->tsnap.snapNormal);
                        }
 
-                       if (RNA_struct_find_property(op->ptr, "use_snap_project"))
-                       {
+                       if (RNA_struct_find_property(op->ptr, "use_snap_project")) {
                                t->tsnap.project = RNA_boolean_get(op->ptr, "use_snap_project");
                        }
 
-                       if (RNA_struct_find_property(op->ptr, "use_snap_self"))
-                       {
+                       if (RNA_struct_find_property(op->ptr, "use_snap_self")) {
                                t->tsnap.snap_self = RNA_boolean_get(op->ptr, "use_snap_self");
                        }
                }
        }
        /* use scene defaults only when transform is modal */
-       else if (t->flag & T_MODAL)
-       {
-               if(ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE))
-               {
+       else if (t->flag & T_MODAL) {
+               if (ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE)) {
                        if (ts->snap_flag & SCE_SNAP) {
                                t->modifiers |= MOD_SNAP;
                        }
@@ -508,8 +479,7 @@ static void setSnappingCallback(TransInfo *t)
 {
        t->tsnap.calcSnap = CalcSnapGeometry;
 
-       switch(t->tsnap.target)
-       {
+       switch (t->tsnap.target) {
                case SCE_SNAP_TARGET_CLOSEST:
                        t->tsnap.targetSnap = TargetSnapClosest;
                        break;
@@ -525,8 +495,7 @@ static void setSnappingCallback(TransInfo *t)
 
        }
 
-       switch (t->mode)
-       {
+       switch (t->mode) {
        case TFM_TRANSLATION:
                t->tsnap.applySnap = ApplySnapTranslation;
                t->tsnap.distance = TranslationBetween;
@@ -580,7 +549,7 @@ int updateSelectedSnapPoint(TransInfo *t)
                int closest_dist = 0;
                int screen_loc[2];
 
-               for( p = t->tsnap.points.first; p; p = p->next ) {
+               for ( p = t->tsnap.points.first; p; p = p->next ) {
                        int dx, dy;
                        int dist;
 
@@ -642,7 +611,8 @@ void getSnapPoint(TransInfo *t, float vec[3])
                }
 
                mul_v3_fl(vec, 1.0f / total);
-       } else {
+       }
+       else {
                copy_v3_v3(vec, t->tsnap.snapPoint);
        }
 }
@@ -656,15 +626,15 @@ static void ApplySnapTranslation(TransInfo *t, float vec[3])
        sub_v3_v3v3(vec, point, t->tsnap.snapTarget);
 }
 
-static void ApplySnapRotation(TransInfo *t, float *vec)
+static void ApplySnapRotation(TransInfo *t, float *value)
 {
        if (t->tsnap.target == SCE_SNAP_TARGET_CLOSEST) {
-               *vec = t->tsnap.dist;
+               *value = t->tsnap.dist;
        }
        else {
                float point[3];
                getSnapPoint(t, point);
-               *vec = RotationBetween(t, t->tsnap.snapTarget, point);
+               *value = RotationBetween(t, t->tsnap.snapTarget, point);
        }
 }
 
@@ -692,7 +662,7 @@ static float RotationBetween(TransInfo *t, float p1[3], float p2[3])
        float angle, start[3], end[3], center[3];
        
        copy_v3_v3(center, t->center);  
-       if(t->flag & (T_EDIT|T_POSE)) {
+       if (t->flag & (T_EDIT|T_POSE)) {
                Object *ob= t->obedit?t->obedit:t->poseobj;
                mul_m4_v3(ob->obmat, center);
        }
@@ -730,7 +700,7 @@ static float RotationBetween(TransInfo *t, float p1[3], float p2[3])
                mul_m3_v3(mtx, end);
                mul_m3_v3(mtx, start);
                
-               angle = atan2(start[1],start[0]) - atan2(end[1],end[0]);
+               angle = atan2(start[1], start[0]) - atan2(end[1], end[0]);
        }
        
        if (angle > (float)M_PI) {
@@ -748,7 +718,7 @@ static float ResizeBetween(TransInfo *t, float p1[3], float p2[3])
        float d1[3], d2[3], center[3], len_d1;
        
        copy_v3_v3(center, t->center);  
-       if(t->flag & (T_EDIT|T_POSE)) {
+       if (t->flag & (T_EDIT|T_POSE)) {
                Object *ob= t->obedit?t->obedit:t->poseobj;
                mul_m4_v3(ob->obmat, center);
        }
@@ -775,8 +745,7 @@ static void UNUSED_FUNCTION(CalcSnapGrid)(TransInfo *t, float *UNUSED(vec))
 
 static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
 {
-       if (t->spacetype == SPACE_VIEW3D)
-       {
+       if (t->spacetype == SPACE_VIEW3D) {
                float loc[3];
                float no[3];
                float mval[2];
@@ -786,8 +755,7 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
                mval[0] = t->mval[0];
                mval[1] = t->mval[1];
                
-               if (t->tsnap.mode == SCE_SNAP_MODE_VOLUME)
-               {
+               if (t->tsnap.mode == SCE_SNAP_MODE_VOLUME) {
                        ListBase depth_peels;
                        DepthPeel *p1, *p2;
                        float *last_p = NULL;
@@ -796,7 +764,7 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
                        
                        depth_peels.first = depth_peels.last = NULL;
                        
-                       peelObjectsTransForm(t, &depth_peels, mval);
+                       peelObjectsTransForm(t, &depth_peels, mval, t->tsnap.modeSelect);
                        
 //                     if (LAST_SNAP_POINT_VALID)
 //                     {
@@ -808,10 +776,8 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
 //                     }
                        
                        
-                       for (p1 = depth_peels.first; p1; p1 = p1->next)
-                       {
-                               if (p1->flag == 0)
-                               {
+                       for (p1 = depth_peels.first; p1; p1 = p1->next) {
+                               if (p1->flag == 0) {
                                        float vec[3];
                                        float new_dist;
                                        
@@ -819,41 +785,33 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
                                        p1->flag = 1;
                
                                        /* if peeling objects, take the first and last from each object */                      
-                                       if (t->settings->snap_flag & SCE_SNAP_PEEL_OBJECT)
-                                       {
+                                       if (t->settings->snap_flag & SCE_SNAP_PEEL_OBJECT) {
                                                DepthPeel *peel;
-                                               for (peel = p1->next; peel; peel = peel->next)
-                                               {
-                                                       if (peel->ob == p1->ob)
-                                                       {
+                                               for (peel = p1->next; peel; peel = peel->next) {
+                                                       if (peel->ob == p1->ob) {
                                                                peel->flag = 1;
                                                                p2 = peel;
                                                        }
                                                }
                                        }
                                        /* otherwise, pair first with second and so on */
-                                       else
-                                       {
-                                               for (p2 = p1->next; p2 && p2->ob != p1->ob; p2 = p2->next)
-                                               {
+                                       else {
+                                               for (p2 = p1->next; p2 && p2->ob != p1->ob; p2 = p2->next) {
                                                        /* nothing to do here */
                                                }
                                        }
                                        
-                                       if (p2)
-                                       {
+                                       if (p2) {
                                                p2->flag = 1;
                                                
                                                add_v3_v3v3(vec, p1->p, p2->p);
                                                mul_v3_fl(vec, 0.5f);
                                        }
-                                       else
-                                       {
+                                       else {
                                                copy_v3_v3(vec, p1->p);
                                        }
-                                       
-                                       if (last_p == NULL)
-                                       {
+
+                                       if (last_p == NULL) {
                                                copy_v3_v3(p, vec);
                                                max_dist = 0;
                                                break;
@@ -861,16 +819,14 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
                                        
                                        new_dist = len_v3v3(last_p, vec);
                                        
-                                       if (new_dist < max_dist)
-                                       {
+                                       if (new_dist < max_dist) {
                                                copy_v3_v3(p, vec);
                                                max_dist = new_dist;
                                        }
                                }
                        }
                        
-                       if (max_dist != FLT_MAX)
-                       {
+                       if (max_dist != FLT_MAX) {
                                copy_v3_v3(loc, p);
                                /* XXX, is there a correct normal in this case ???, for now just z up */
                                no[0]= 0.0;
@@ -881,20 +837,17 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
                        
                        BLI_freelistN(&depth_peels);
                }
-               else
-               {
+               else {
                        found = snapObjectsTransform(t, mval, &dist, loc, no, t->tsnap.modeSelect);
                }
                
-               if (found == 1)
-               {
+               if (found == 1) {
                        float tangent[3];
                        
                        sub_v3_v3v3(tangent, loc, t->tsnap.snapPoint);
                        tangent[2] = 0; 
                        
-                       if (dot_v3v3(tangent, tangent) > 0)
-                       {
+                       if (dot_v3v3(tangent, tangent) > 0) {
                                copy_v3_v3(t->tsnap.snapTangent, tangent);
                        }
                        
@@ -903,29 +856,25 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
 
                        t->tsnap.status |=  POINT_INIT;
                }
-               else
-               {
+               else {
                        t->tsnap.status &= ~POINT_INIT;
                }
        }
-       else if (t->spacetype == SPACE_IMAGE && t->obedit != NULL && t->obedit->type==OB_MESH)
-       {       /* same as above but for UV's */
+       else if (t->spacetype == SPACE_IMAGE && t->obedit != NULL && t->obedit->type==OB_MESH) {
                /* same as above but for UV's */
                Image *ima= ED_space_image(t->sa->spacedata.first);
                float aspx, aspy, co[2];
                
                UI_view2d_region_to_view(&t->ar->v2d, t->mval[0], t->mval[1], co, co+1);
 
-               if(ED_uvedit_nearest_uv(t->scene, t->obedit, ima, co, t->tsnap.snapPoint))
-               {
+               if (ED_uvedit_nearest_uv(t->scene, t->obedit, ima, co, t->tsnap.snapPoint)) {
                        ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
                        t->tsnap.snapPoint[0] *= aspx;
                        t->tsnap.snapPoint[1] *= aspy;
 
                        t->tsnap.status |=  POINT_INIT;
                }
-               else
-               {
+               else {
                        t->tsnap.status &= ~POINT_INIT;
                }
        }
@@ -935,11 +884,10 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
 
 static void TargetSnapCenter(TransInfo *t)
 {
-       // Only need to calculate once
-       if ((t->tsnap.status & TARGET_INIT) == 0)
-       {
+       /* Only need to calculate once */
+       if ((t->tsnap.status & TARGET_INIT) == 0) {
                copy_v3_v3(t->tsnap.snapTarget, t->center);     
-               if(t->flag & (T_EDIT|T_POSE)) {
+               if (t->flag & (T_EDIT|T_POSE)) {
                        Object *ob= t->obedit?t->obedit:t->poseobj;
                        mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
                }
@@ -950,27 +898,23 @@ static void TargetSnapCenter(TransInfo *t)
 
 static void TargetSnapActive(TransInfo *t)
 {
-       // Only need to calculate once
-       if ((t->tsnap.status & TARGET_INIT) == 0)
-       {
+       /* Only need to calculate once */
+       if ((t->tsnap.status & TARGET_INIT) == 0) {
                TransData *td = NULL;
                TransData *active_td = NULL;
                int i;
 
-               for(td = t->data, i = 0 ; i < t->total && td->flag & TD_SELECTED ; i++, td++)
-               {
-                       if (td->flag & TD_ACTIVE)
-                       {
+               for (td = t->data, i = 0 ; i < t->total && td->flag & TD_SELECTED ; i++, td++) {
+                       if (td->flag & TD_ACTIVE) {
                                active_td = td;
                                break;
                        }
                }
 
-               if (active_td)
-               {       
+               if (active_td) {
                        copy_v3_v3(t->tsnap.snapTarget, active_td->center);
                                
-                       if(t->flag & (T_EDIT|T_POSE)) {
+                       if (t->flag & (T_EDIT|T_POSE)) {
                                Object *ob= t->obedit?t->obedit:t->poseobj;
                                mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
                        }
@@ -978,8 +922,7 @@ static void TargetSnapActive(TransInfo *t)
                        t->tsnap.status |= TARGET_INIT;
                }
                /* No active, default to median */
-               else
-               {
+               else {
                        t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
                        t->tsnap.targetSnap = TargetSnapMedian;
                        TargetSnapMedian(t);
@@ -990,8 +933,7 @@ static void TargetSnapActive(TransInfo *t)
 static void TargetSnapMedian(TransInfo *t)
 {
        // Only need to calculate once
-       if ((t->tsnap.status & TARGET_INIT) == 0)
-       {
+       if ((t->tsnap.status & TARGET_INIT) == 0) {
                TransData *td = NULL;
                int i;
 
@@ -999,14 +941,13 @@ static void TargetSnapMedian(TransInfo *t)
                t->tsnap.snapTarget[1] = 0;
                t->tsnap.snapTarget[2] = 0;
                
-               for(td = t->data, i = 0 ; i < t->total && td->flag & TD_SELECTED ; i++, td++)
-               {
+               for (td = t->data, i = 0 ; i < t->total && td->flag & TD_SELECTED ; i++, td++) {
                        add_v3_v3(t->tsnap.snapTarget, td->center);
                }
                
                mul_v3_fl(t->tsnap.snapTarget, 1.0 / i);
                
-               if(t->flag & (T_EDIT|T_POSE)) {
+               if (t->flag & (T_EDIT|T_POSE)) {
                        Object *ob= t->obedit?t->obedit:t->poseobj;
                        mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
                }
@@ -1018,21 +959,17 @@ static void TargetSnapMedian(TransInfo *t)
 static void TargetSnapClosest(TransInfo *t)
 {
        // Only valid if a snap point has been selected
-       if (t->tsnap.status & POINT_INIT)
-       {
+       if (t->tsnap.status & POINT_INIT) {
                TransData *closest = NULL, *td = NULL;
                
                /* Object mode */
-               if (t->flag & T_OBJECT)
-               {
+               if (t->flag & T_OBJECT) {
                        int i;
-                       for(td = t->data, i = 0 ; i < t->total && td->flag & TD_SELECTED ; i++, td++)
-                       {
-                               struct BoundBox *bb = object_get_boundbox(td->ob);
+                       for (td = t->data, i = 0 ; i < t->total && td->flag & TD_SELECTED ; i++, td++) {
+                               struct BoundBox *bb = BKE_object_boundbox_get(td->ob);
                                
                                /* use boundbox if possible */
-                               if (bb)
-                               {
+                               if (bb) {
                                        int j;
                                        
                                        for (j = 0; j < 8; j++) {
@@ -1044,8 +981,7 @@ static void TargetSnapClosest(TransInfo *t)
                                                
                                                dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
                                                
-                                               if (closest == NULL || fabs(dist) < fabs(t->tsnap.dist))
-                                               {
+                                               if (closest == NULL || fabs(dist) < fabs(t->tsnap.dist)) {
                                                        copy_v3_v3(t->tsnap.snapTarget, loc);
                                                        closest = td;
                                                        t->tsnap.dist = dist; 
@@ -1053,8 +989,7 @@ static void TargetSnapClosest(TransInfo *t)
                                        }
                                }
                                /* use element center otherwise */
-                               else
-                               {
+                               else {
                                        float loc[3];
                                        float dist;
                                        
@@ -1062,8 +997,7 @@ static void TargetSnapClosest(TransInfo *t)
                                        
                                        dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
                                        
-                                       if (closest == NULL || fabs(dist) < fabs(t->tsnap.dist))
-                                       {
+                                       if (closest == NULL || fabs(dist) < fabs(t->tsnap.dist)) {
                                                copy_v3_v3(t->tsnap.snapTarget, loc);
                                                closest = td;
                                                t->tsnap.dist = dist; 
@@ -1071,25 +1005,22 @@ static void TargetSnapClosest(TransInfo *t)
                                }
                        }
                }
-               else
-               {
+               else {
                        int i;
-                       for(td = t->data, i = 0 ; i < t->total && td->flag & TD_SELECTED ; i++, td++)
-                       {
+                       for (td = t->data, i = 0 ; i < t->total && td->flag & TD_SELECTED ; i++, td++) {
                                float loc[3];
                                float dist;
                                
                                copy_v3_v3(loc, td->center);
                                
-                               if(t->flag & (T_EDIT|T_POSE)) {
+                               if (t->flag & (T_EDIT|T_POSE)) {
                                        Object *ob= t->obedit?t->obedit:t->poseobj;
                                        mul_m4_v3(ob->obmat, loc);
                                }
                                
                                dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
                                
-                               if (closest == NULL || fabs(dist) < fabs(t->tsnap.dist))
-                               {
+                               if (closest == NULL || fabs(dist) < fabs(t->tsnap.dist)) {
                                        copy_v3_v3(t->tsnap.snapTarget, loc);
                                        closest = td;
                                        t->tsnap.dist = dist; 
@@ -1124,9 +1055,9 @@ static int snapFace(ARegion *ar, float v1co[3], float v2co[3], float v3co[3], fl
                copy_v3_v3(location, intersect);
                
                if (v4co)
-                       normal_quad_v3( normal,v1co, v2co, v3co, v4co);
+                       normal_quad_v3(normal, v1co, v2co, v3co, v4co);
                else
-                       normal_tri_v3( normal,v1co, v2co, v3co);
+                       normal_tri_v3(normal, v1co, v2co, v3co);
 
                mul_m4_v3(obmat, location);
                
@@ -1135,8 +1066,7 @@ static int snapFace(ARegion *ar, float v1co[3], float v2co[3], float v3co[3], fl
                project_int(ar, location, screen_loc);
                new_dist = abs(screen_loc[0] - (int)mval[0]) + abs(screen_loc[1] - (int)mval[1]);
                
-               if (new_dist <= *dist && new_depth < *depth) 
-               {
+               if (new_dist <= *dist && new_depth < *depth)  {
                        *depth = new_depth;
                        retval = 1;
                        
@@ -1168,8 +1098,7 @@ static int snapEdge(ARegion *ar, float v1co[3], short v1no[3], float v2co[3], sh
        
        result = isect_line_line_v3(v1co, v2co, ray_start_local, ray_end, intersect, dvec); /* dvec used but we don't care about result */
        
-       if (result)
-       {
+       if (result) {
                float edge_loc[3], vec[3];
                float mul;
        
@@ -1190,8 +1119,7 @@ static int snapEdge(ARegion *ar, float v1co[3], short v1no[3], float v2co[3], sh
                        copy_v3_v3(intersect, v2co);
                }
 
-               if (dot_v3v3(ray_normal_local, dvec) > 0)
-               {
+               if (dot_v3v3(ray_normal_local, dvec) > 0) {
                        float location[3];
                        float new_depth;
                        int screen_loc[2];
@@ -1210,8 +1138,7 @@ static int snapEdge(ARegion *ar, float v1co[3], short v1no[3], float v2co[3], sh
                         * this takes care of series of connected edges a bit slanted w.r.t the viewport
                         * otherwise, it would stick to the verts of the closest edge and not slide along merrily 
                         * */
-                       if (new_dist <= *r_dist && new_depth < *r_depth * 1.001f)
-                       {
+                       if (new_dist <= *r_dist && new_depth < *r_depth * 1.001f) {
                                float n1[3], n2[3];
                                
                                *r_depth = new_depth;
@@ -1222,8 +1149,7 @@ static int snapEdge(ARegion *ar, float v1co[3], short v1no[3], float v2co[3], sh
                                
                                mul = dot_v3v3(vec, edge_loc) / dot_v3v3(edge_loc, edge_loc);
                                
-                               if (r_no)
-                               {
+                               if (r_no) {
                                        normal_short_to_float_v3(n1, v1no);                                             
                                        normal_short_to_float_v3(n2, v2no);
                                        interp_v3_v3v3(r_no, n2, n1, mul);
@@ -1250,8 +1176,7 @@ static int snapVertex(ARegion *ar, float vco[3], short vno[3], float obmat[][4],
        
        sub_v3_v3v3(dvec, vco, ray_start_local);
        
-       if (dot_v3v3(ray_normal_local, dvec) > 0)
-       {
+       if (dot_v3v3(ray_normal_local, dvec) > 0) {
                float location[3];
                float new_depth;
                int screen_loc[2];
@@ -1266,15 +1191,13 @@ static int snapVertex(ARegion *ar, float vco[3], short vno[3], float obmat[][4],
                project_int(ar, location, screen_loc);
                new_dist = abs(screen_loc[0] - (int)mval[0]) + abs(screen_loc[1] - (int)mval[1]);
                
-               if (new_dist <= *r_dist && new_depth < *r_depth)
-               {
+               if (new_dist <= *r_dist && new_depth < *r_depth) {
                        *r_depth = new_depth;
                        retval = 1;
                        
                        copy_v3_v3(r_loc, location);
                        
-                       if (r_no)
-                       {
+                       if (r_no) {
                                normal_short_to_float_v3(r_no, vno);
                                mul_m3_v3(timat, r_no);
                                normalize_v3(r_no);
@@ -1303,16 +1226,14 @@ static int snapArmature(short snap_mode, ARegion *ar, Object *ob, bArmature *arm
        mul_m4_v3(imat, ray_start_local);
        mul_mat3_m4_v3(imat, ray_normal_local);
 
-       if(arm->edbo)
-       {
+       if (arm->edbo) {
                EditBone *eBone;
 
                for (eBone=arm->edbo->first; eBone; eBone=eBone->next) {
                        if (eBone->layer & arm->layer) {
                                /* skip hidden or moving (selected) bones */
                                if ((eBone->flag & (BONE_HIDDEN_A|BONE_ROOTSEL|BONE_TIPSEL))==0) {
-                                       switch (snap_mode)
-                                       {
+                                       switch (snap_mode) {
                                                case SCE_SNAP_MODE_VERTEX:
                                                        retval |= snapVertex(ar, eBone->head, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
                                                        retval |= snapVertex(ar, eBone->tail, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
@@ -1325,8 +1246,7 @@ static int snapArmature(short snap_mode, ARegion *ar, Object *ob, bArmature *arm
                        }
                }
        }
-       else if (ob->pose && ob->pose->chanbase.first)
-       {
+       else if (ob->pose && ob->pose->chanbase.first) {
                bPoseChannel *pchan;
                Bone *bone;
                
@@ -1337,8 +1257,7 @@ static int snapArmature(short snap_mode, ARegion *ar, Object *ob, bArmature *arm
                                float *head_vec = pchan->pose_head;
                                float *tail_vec = pchan->pose_tail;
                                
-                               switch (snap_mode)
-                               {
+                               switch (snap_mode) {
                                        case SCE_SNAP_MODE_VERTEX:
                                                retval |= snapVertex(ar, head_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
                                                retval |= snapVertex(ar, tail_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
@@ -1384,14 +1303,13 @@ static int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh
                 * test against boundbox first
                 * */
                if (totface > 16) {
-                       struct BoundBox *bb = object_get_boundbox(ob);
-                       test = ray_hit_boundbox(bb, ray_start_local, ray_normal_local);
+                       struct BoundBox *bb = BKE_object_boundbox_get(ob);
+                       test = BKE_boundbox_ray_hit_check(bb, ray_start_local, ray_normal_local);
                }
                
                if (test == 1) {
                        
-                       switch (snap_mode)
-                       {
+                       switch (snap_mode) {
                                case SCE_SNAP_MODE_FACE:
                                { 
 #ifdef USE_BVH_FACE_SNAP                               // Added for durian
@@ -1407,9 +1325,8 @@ static int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh
                                        hit.index = -1;
                                        hit.dist = *r_depth * (*r_depth == FLT_MAX ? 1.0f : local_scale);
 
-                                       if(treeData.tree && BLI_bvhtree_ray_cast(treeData.tree, ray_start_local, ray_normal_local, 0.0f, &hit, treeData.raycast_callback, &treeData) != -1)
-                                       {
-                                               if(hit.dist/local_scale <= *r_depth) {
+                                       if (treeData.tree && BLI_bvhtree_ray_cast(treeData.tree, ray_start_local, ray_normal_local, 0.0f, &hit, treeData.raycast_callback, &treeData) != -1) {
+                                               if (hit.dist/local_scale <= *r_depth) {
                                                        *r_depth= hit.dist/local_scale;
                                                        copy_v3_v3(r_loc, hit.co);
                                                        copy_v3_v3(r_no, hit.no);
@@ -1433,47 +1350,41 @@ static int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh
                                        int index = 0;
                                        int i;
                                        
-                                       if (em != NULL)
-                                       {
+                                       if (em != NULL) {
                                                index_array = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
-                                               EDBM_init_index_arrays(em, 0, 0, 1);
+                                               EDBM_index_arrays_init(em, 0, 0, 1);
                                        }
                                        
-                                       for( i = 0; i < totface; i++) {
+                                       for ( i = 0; i < totface; i++) {
                                                BMFace *efa = NULL;
                                                MFace *f = faces + i;
                                                
                                                test = 1; /* reset for every face */
                                        
-                                               if (em != NULL)
-                                               {
-                                                       if (index_array)
-                                                       {
+                                               if (em != NULL) {
+                                                       if (index_array) {
                                                                index = index_array[i];
                                                        }
-                                                       else
-                                                       {
+                                                       else {
                                                                index = i;
                                                        }
                                                        
-                                                       if (index == ORIGINDEX_NONE)
-                                                       {
+                                                       if (index == ORIGINDEX_NONE) {
                                                                test = 0;
                                                        }
-                                                       else
-                                                       {
-                                                               efa = EDBM_get_face_for_index(em, index);
+                                                       else {
+                                                               efa = EDBM_face_at_index(em, index);
                                                                
-                                                               if (efa && BM_TestHFlag(efa, BM_HIDDEN))
-                                                               {
+                                                               if (efa && BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
                                                                        test = 0;
-                                                               } else if (efa) {
+                                                               }
+                                                               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)) {
+                                                                       l = BM_iter_new(&iter, em->bm, BM_LOOPS_OF_FACE, efa);
+                                                                       for ( ; l; l=BM_iter_step(&iter)) {
+                                                                               if (BM_elem_flag_test(l->v, BM_ELEM_SELECT)) {
                                                                                        test = 0;
                                                                                        break;
                                                                                }
@@ -1483,29 +1394,25 @@ static int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh
                                                }
                                                
                                                
-                                               if (test)
-                                               {
+                                               if (test) {
                                                        int result;
                                                        float *v4co = NULL;
                                                        
-                                                       if (f->v4)
-                                                       {
+                                                       if (f->v4) {
                                                                v4co = verts[f->v4].co;
                                                        }
                                                        
                                                        result = snapFace(ar, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co, v4co, mval, ray_start, ray_start_local, ray_normal_local, obmat, timat, loc, no, dist, depth);
                                                        retval |= result;
 
-                                                       if (f->v4 && result == 0)
-                                                       {
+                                                       if (f->v4 && result == 0) {
                                                                retval |= snapFace(ar, verts[f->v3].co, verts[f->v4].co, verts[f->v1].co, verts[f->v2].co, mval, ray_start, ray_start_local, ray_normal_local, obmat, timat, loc, no, dist, depth);
                                                        }
                                                }
                                        }
                                        
-                                       if (em != NULL)
-                                       {
-                                               EDBM_free_index_arrays(em);
+                                       if (em != NULL) {
+                                               EDBM_index_arrays_free(em);
                                        }
 #endif
                                        break;
@@ -1517,54 +1424,45 @@ static int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh
                                        int index = 0;
                                        int i;
                                        
-                                       if (em != NULL)
-                                       {
+                                       if (em != NULL) {
                                                index_array = dm->getVertDataArray(dm, CD_ORIGINDEX);
-                                               EDBM_init_index_arrays(em, 1, 0, 0);
+                                               EDBM_index_arrays_init(em, 1, 0, 0);
                                        }
                                        
-                                       for( i = 0; i < totvert; i++) {
+                                       for ( i = 0; i < totvert; i++) {
                                                BMVert *eve = NULL;
                                                MVert *v = verts + i;
                                                
                                                test = 1; /* reset for every vert */
                                        
-                                               if (em != NULL)
-                                               {
-                                                       if (index_array)
-                                                       {
+                                               if (em != NULL) {
+                                                       if (index_array) {
                                                                index = index_array[i];
                                                        }
-                                                       else
-                                                       {
+                                                       else {
                                                                index = i;
                                                        }
                                                        
-                                                       if (index == ORIGINDEX_NONE)
-                                                       {
+                                                       if (index == ORIGINDEX_NONE) {
                                                                test = 0;
                                                        }
-                                                       else
-                                                       {
-                                                               eve = EDBM_get_vert_for_index(em, index);
+                                                       else {
+                                                               eve = EDBM_vert_at_index(em, index);
                                                                
-                                                               if (eve && (BM_TestHFlag(eve, BM_HIDDEN) || BM_TestHFlag(eve, BM_SELECT)))
-                                                               {
+                                                               if (eve && (BM_elem_flag_test(eve, BM_ELEM_HIDDEN) || BM_elem_flag_test(eve, BM_ELEM_SELECT))) {
                                                                        test = 0;
                                                                }
                                                        }
                                                }
                                                
                                                
-                                               if (test)
-                                               {
+                                               if (test) {
                                                        retval |= snapVertex(ar, v->co, v->no, obmat, timat, ray_start, ray_start_local, ray_normal_local, mval, r_loc, r_no, r_dist, r_depth);
                                                }
                                        }
 
-                                       if (em != NULL)
-                                       {
-                                               EDBM_free_index_arrays(em);
+                                       if (em != NULL) {
+                                               EDBM_index_arrays_free(em);
                                        }
                                        break;
                                }
@@ -1577,56 +1475,47 @@ static int snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh
                                        int index = 0;
                                        int i;
                                        
-                                       if (em != NULL)
-                                       {
+                                       if (em != NULL) {
                                                index_array = dm->getEdgeDataArray(dm, CD_ORIGINDEX);
-                                               EDBM_init_index_arrays(em, 0, 1, 0);
+                                               EDBM_index_arrays_init(em, 0, 1, 0);
                                        }
                                        
-                                       for( i = 0; i < totedge; i++) {
+                                       for ( i = 0; i < totedge; i++) {
                                                BMEdge *eed = NULL;
                                                MEdge *e = edges + i;
                                                
                                                test = 1; /* reset for every vert */
                                        
-                                               if (em != NULL)
-                                               {
-                                                       if (index_array)
-                                                       {
+                                               if (em != NULL) {
+                                                       if (index_array) {
                                                                index = index_array[i];
                                                        }
-                                                       else
-                                                       {
+                                                       else {
                                                                index = i;
                                                        }
                                                        
-                                                       if (index == ORIGINDEX_NONE)
-                                                       {
+                                                       if (index == ORIGINDEX_NONE) {
                                                                test = 0;
                                                        }
-                                                       else
-                                                       {
-                                                               eed = EDBM_get_edge_for_index(em, index);
+                                                       else {
+                                                               eed = EDBM_edge_at_index(em, index);
                                                                
-                                                               if (eed && (BM_TestHFlag(eed, BM_HIDDEN) ||
-                                                                       BM_TestHFlag(eed->v1, BM_SELECT) || 
-                                                                       BM_TestHFlag(eed->v2, BM_SELECT)))
+                                                               if (eed && (BM_elem_flag_test(eed, BM_ELEM_HIDDEN) ||
+                                                                       BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) || 
+                                                                       BM_elem_flag_test(eed->v2, BM_ELEM_SELECT)))
                                                                {
                                                                        test = 0;
                                                                }
                                                        }
                                                }
-                                               
-                                               
-                                               if (test)
-                                               {
+
+                                               if (test) {
                                                        retval |= snapEdge(ar, verts[e->v1].co, verts[e->v1].no, verts[e->v2].co, verts[e->v2].no, obmat, timat, ray_start, ray_start_local, ray_normal_local, mval, r_loc, r_no, r_dist, r_depth);
                                                }
                                        }
 
-                                       if (em != NULL)
-                                       {
-                                               EDBM_free_index_arrays(em);
+                                       if (em != NULL) {
+                                               EDBM_index_arrays_free(em);
                                        }
                                        break;
                                }
@@ -1648,14 +1537,12 @@ static int snapObject(Scene *scene, ARegion *ar, Object *ob, int editobject, flo
                BMEditMesh *em;
                DerivedMesh *dm;
                
-               if (editobject)
-               {
-                       em = ((Mesh *)ob->data)->edit_btmesh;
+               if (editobject) {
+                       em = BMEdit_FromObject(ob);
                        /* dm = editbmesh_get_derived_cage(scene, ob, em, CD_MASK_BAREMESH); */
                        dm = editbmesh_get_derived_base(ob, em); /* limitation, em & dm MUST have the same number of faces */
                }
-               else
-               {
+               else {
                        em = NULL;
                        dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
                }
@@ -1664,8 +1551,7 @@ static int snapObject(Scene *scene, ARegion *ar, Object *ob, int editobject, flo
 
                dm->release(dm);
        }
-       else if (ob->type == OB_ARMATURE)
-       {
+       else if (ob->type == OB_ARMATURE) {
                retval = snapArmature(ts->snap_mode, ar, ob, ob->data, obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, r_depth);
        }
        
@@ -1682,8 +1568,7 @@ static int snapObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, c
        
        ED_view3d_win_to_ray(ar, v3d, mval, ray_start, ray_normal);
 
-       if (mode == SNAP_ALL && obedit)
-       {
+       if (mode == SNAP_ALL && obedit) {
                Object *ob = obedit;
 
                retval |= snapObject(scene, ar, ob, 1, ob->obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, &depth);
@@ -1695,8 +1580,7 @@ static int snapObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, c
         * To solve that problem, we do it first as an exception. 
         * */
        base= BASACT;
-       if(base && base->object && base->object->mode & OB_MODE_PARTICLE_EDIT)
-       {
+       if (base && base->object && base->object->mode & OB_MODE_PARTICLE_EDIT) {
                Object *ob = base->object;
                retval |= snapObject(scene, ar, ob, 0, ob->obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, &depth);
        }
@@ -1710,13 +1594,11 @@ static int snapObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, c
                {
                        Object *ob = base->object;
                        
-                       if (ob->transflag & OB_DUPLI)
-                       {
+                       if (ob->transflag & OB_DUPLI) {
                                DupliObject *dupli_ob;
                                ListBase *lb = object_duplilist(scene, ob);
                                
-                               for(dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next)
-                               {
+                               for (dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next) {
                                        Object *dob = dupli_ob->ob;
                                        
                                        retval |= snapObject(scene, ar, dob, 0, dupli_ob->mat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, &depth);
@@ -1754,12 +1636,10 @@ static int cmpPeel(void *arg1, void *arg2)
        DepthPeel *p2 = arg2;
        int val = 0;
        
-       if (p1->depth < p2->depth)
-       {
+       if (p1->depth < p2->depth) {
                val = -1;
        }
-       else if (p1->depth > p2->depth)
-       {
+       else if (p1->depth > p2->depth) {
                val = 1;
        }
        
@@ -1770,16 +1650,13 @@ static void removeDoublesPeel(ListBase *depth_peels)
 {
        DepthPeel *peel;
        
-       for (peel = depth_peels->first; peel; peel = peel->next)
-       {
+       for (peel = depth_peels->first; peel; peel = peel->next) {
                DepthPeel *next_peel = peel->next;
-               
-               if (next_peel && ABS(peel->depth - next_peel->depth) < 0.0015f)
-               {
+
+               if (next_peel && ABS(peel->depth - next_peel->depth) < 0.0015f) {
                        peel->next = next_peel->next;
                        
-                       if (next_peel->next)
-                       {
+                       if (next_peel->next) {
                                next_peel->next->prev = peel;
                        }
                        
@@ -1832,8 +1709,8 @@ static int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4],
                 * test against boundbox first
                 * */
                if (totface > 16) {
-                       struct BoundBox *bb = object_get_boundbox(ob);
-                       test = ray_hit_boundbox(bb, ray_start_local, ray_normal_local);
+                       struct BoundBox *bb = BKE_object_boundbox_get(ob);
+                       test = BKE_boundbox_ray_hit_check(bb, ray_start_local, ray_normal_local);
                }
                
                if (test == 1) {
@@ -1841,7 +1718,7 @@ static int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4],
                        MFace *faces = dm->getTessFaceArray(dm);
                        int i;
                        
-                       for( i = 0; i < totface; i++) {
+                       for ( i = 0; i < totface; i++) {
                                MFace *f = faces + i;
                                float lambda;
                                int result;
@@ -1861,9 +1738,9 @@ static int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4],
                                        copy_v3_v3(location, intersect);
                                        
                                        if (f->v4)
-                                               normal_quad_v3( normal,verts[f->v1].co, verts[f->v2].co, verts[f->v3].co, verts[f->v4].co);
+                                               normal_quad_v3(normal, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co, verts[f->v4].co);
                                        else
-                                               normal_tri_v3( normal,verts[f->v1].co, verts[f->v2].co, verts[f->v3].co);
+                                               normal_tri_v3(normal, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co);
 
                                        mul_m4_v3(obmat, location);
                                        
@@ -1875,8 +1752,7 @@ static int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4],
                                        addDepthPeel(depth_peels, new_depth, location, normal, ob);
                                }
                
-                               if (f->v4 && result == 0)
-                               {
+                               if (f->v4 && result == 0) {
                                        result = isect_ray_tri_threshold_v3(ray_start_local, ray_normal_local, verts[f->v3].co, verts[f->v4].co, verts[f->v1].co, &lambda, NULL, 0.001);
                                        
                                        if (result) {
@@ -1891,13 +1767,13 @@ static int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4],
                                                copy_v3_v3(location, intersect);
                                                
                                                if (f->v4)
-                                                       normal_quad_v3( normal,verts[f->v1].co, verts[f->v2].co, verts[f->v3].co, verts[f->v4].co);
+                                                       normal_quad_v3(normal, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co, verts[f->v4].co);
                                                else
-                                                       normal_tri_v3( normal,verts[f->v1].co, verts[f->v2].co, verts[f->v3].co);
+                                                       normal_tri_v3(normal, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co);
 
                                                mul_m4_v3(obmat, location);
                                                
-                                               new_depth = len_v3v3(location, ray_start);                                      
+                                               new_depth = len_v3v3(location, ray_start);
                                                
                                                mul_m3_v3(timat, normal);
                                                normalize_v3(normal);
@@ -1912,7 +1788,7 @@ static int peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[][4],
        return retval;
 } 
 
-static int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, ListBase *depth_peels, const float mval[2])
+static int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, ListBase *depth_peels, const float mval[2], SnapMode mode)
 {
        Base *base;
        int retval = 0;
@@ -1920,35 +1796,30 @@ static int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, L
        
        ED_view3d_win_to_ray(ar, v3d, mval, ray_start, ray_normal);
 
-       for ( base = scene->base.first; base != NULL; base = base->next ) {
-               if ( BASE_SELECTABLE(v3d, base) ) {
+       for (base = scene->base.first; base != NULL; base = base->next) {
+               if (BASE_SELECTABLE(v3d, base)) {
                        Object *ob = base->object;
-                       
-                       if (ob->transflag & OB_DUPLI)
-                       {
+
+                       if (ob->transflag & OB_DUPLI) {
                                DupliObject *dupli_ob;
                                ListBase *lb = object_duplilist(scene, ob);
                                
-                               for(dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next)
-                               {
+                               for (dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next) {
                                        Object *dob = dupli_ob->ob;
                                        
                                        if (dob->type == OB_MESH) {
-#if 0 //BMESH_TODO
-                                               EditMesh *em;
+                                               BMEditMesh *em;
                                                DerivedMesh *dm = NULL;
                                                int val;
 
-                                               if (dob != obedit)
-                                               {
+                                               if (dob != obedit) {
                                                        dm = mesh_get_derived_final(scene, dob, CD_MASK_BAREMESH);
                                                        
                                                        val = peelDerivedMesh(dob, dm, dob->obmat, ray_start, ray_normal, mval, depth_peels);
                                                }
-                                               else
-                                               {
-                                                       em = ((Mesh *)dob->data)->edit_mesh;
-                                                       dm = editmesh_get_derived_cage(scene, obedit, em, CD_MASK_BAREMESH);
+                                               else {
+                                                       em = BMEdit_FromObject(dob);
+                                                       dm = editbmesh_get_derived_cage(scene, obedit, em, CD_MASK_BAREMESH);
                                                        
                                                        val = peelDerivedMesh(dob, dm, dob->obmat, ray_start, ray_normal, mval, depth_peels);
                                                }
@@ -1956,7 +1827,6 @@ static int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, L
                                                retval = retval || val;
                                                
                                                dm->release(dm);
-#endif
                                        }
                                }
                                
@@ -1964,27 +1834,24 @@ static int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, L
                        }
                        
                        if (ob->type == OB_MESH) {
-                               BMEditMesh *em;
-                               DerivedMesh *dm = NULL;
-                               int val;
+                               int val = 0;
 
-                               if (ob != obedit)
-                               {
-                                       dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
+                               if (ob != obedit && ((mode == SNAP_NOT_SELECTED && (base->flag & (SELECT|BA_WAS_SEL)) == 0) || ELEM(mode, SNAP_ALL, SNAP_NOT_OBEDIT))) {
+                                       DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
                                        
                                        val = peelDerivedMesh(ob, dm, ob->obmat, ray_start, ray_normal, mval, depth_peels);
+                                       dm->release(dm);
                                }
-                               else
-                               {
-                                       em = ((Mesh *)ob->data)->edit_btmesh;
-                                       dm = editbmesh_get_derived_cage(scene, obedit, em, CD_MASK_BAREMESH);
+                               else if (ob == obedit && mode != SNAP_NOT_OBEDIT) {
+                                       BMEditMesh *em = BMEdit_FromObject(ob);
+                                       DerivedMesh *dm = editbmesh_get_derived_cage(scene, obedit, em, CD_MASK_BAREMESH);
                                        
                                        val = peelDerivedMesh(ob, dm, ob->obmat, ray_start, ray_normal, mval, depth_peels);
+                                       dm->release(dm);
                                }
                                        
                                retval = retval || val;
                                
-                               dm->release(dm);
                        }
                }
        }
@@ -1995,17 +1862,17 @@ static int peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, L
        return retval;
 }
 
-int peelObjectsTransForm(TransInfo *t, ListBase *depth_peels, const float mval[2])
+int peelObjectsTransForm(TransInfo *t, ListBase *depth_peels, const float mval[2], SnapMode mode)
 {
-       return peelObjects(t->scene, t->view, t->ar, t->obedit, depth_peels, mval);
+       return peelObjects(t->scene, t->view, t->ar, t->obedit, depth_peels, mval, mode);
 }
 
-int peelObjectsContext(bContext *C, ListBase *depth_peels, const float mval[2])
+int peelObjectsContext(bContext *C, ListBase *depth_peels, const float mval[2], SnapMode mode)
 {
        ScrArea *sa = CTX_wm_area(C);
        View3D *v3d = sa->spacedata.first;
 
-       return peelObjects(CTX_data_scene(C), v3d, CTX_wm_region(C), CTX_data_edit_object(C), depth_peels, mval);
+       return peelObjects(CTX_data_scene(C), v3d, CTX_wm_region(C), CTX_data_edit_object(C), depth_peels, mval, mode);
 }
 
 /*================================================================*/
@@ -2048,7 +1915,7 @@ static void applyGrid(TransInfo *t, float *val, int max_index, float fac[3], Gea
        int i;
        float asp[3] = {1.0f, 1.0f, 1.0f}; // TODO: Remove hard coded limit here (3)
 
-       if(max_index > 2) {
+       if (max_index > 2) {
                printf("applyGrid: invalid index %d, clamping\n", max_index);
                max_index= 2;
        }
@@ -2058,7 +1925,7 @@ static void applyGrid(TransInfo *t, float *val, int max_index, float fac[3], Gea
                return;
        
        /* evil hack - snapping needs to be adapted for image aspect ratio */
-       if((t->spacetype==SPACE_IMAGE) && (t->mode==TFM_TRANSLATION)) {
+       if ((t->spacetype==SPACE_IMAGE) && (t->mode==TFM_TRANSLATION)) {
                ED_space_image_uv_aspect(t->sa->spacedata.first, asp, asp+1);
        }