Sculpt:
[blender.git] / source / blender / editors / space_view3d / view3d_edit.c
index 8681a5b..fa69085 100644 (file)
@@ -46,7 +46,7 @@
 #include "MEM_guardedalloc.h"
 
 #include "BLI_blenlib.h"
-#include "BLI_arithb.h"
+#include "BLI_math.h"
 #include "BLI_rand.h"
 
 #include "BKE_action.h"
@@ -94,7 +94,7 @@ static void view3d_boxview_clip(ScrArea *sa)
        ARegion *ar;
        BoundBox *bb = MEM_callocN(sizeof(BoundBox), "clipbb");
        float clip[6][4];
-       float x1= 0.0f, y1= 0.0f, z1= 0.0f, ofs[3];
+       float x1= 0.0f, y1= 0.0f, z1= 0.0f, ofs[3] = {0.0f, 0.0f, 0.0f};
        int val;
 
        /* create bounding box */
@@ -103,7 +103,7 @@ static void view3d_boxview_clip(ScrArea *sa)
                        RegionView3D *rv3d= ar->regiondata;
 
                        if(rv3d->viewlock & RV3D_BOXCLIP) {
-                               if(ELEM(rv3d->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM)) {
+                               if(ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
                                        if(ar->winx>ar->winy) x1= rv3d->dist;
                                        else x1= ar->winx*rv3d->dist/ar->winy;
 
@@ -113,7 +113,7 @@ static void view3d_boxview_clip(ScrArea *sa)
                                        ofs[0]= rv3d->ofs[0];
                                        ofs[1]= rv3d->ofs[1];
                                }
-                               else if(ELEM(rv3d->view, V3D_VIEW_FRONT, V3D_VIEW_BACK)) {
+                               else if(ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
                                        ofs[2]= rv3d->ofs[2];
 
                                        if(ar->winx>ar->winy) z1= ar->winy*rv3d->dist/ar->winx;
@@ -141,12 +141,12 @@ static void view3d_boxview_clip(ScrArea *sa)
        }
 
        /* normals for plane equations */
-       CalcNormFloat(bb->vec[0], bb->vec[1], bb->vec[4], clip[0]);
-       CalcNormFloat(bb->vec[1], bb->vec[2], bb->vec[5], clip[1]);
-       CalcNormFloat(bb->vec[2], bb->vec[3], bb->vec[6], clip[2]);
-       CalcNormFloat(bb->vec[3], bb->vec[0], bb->vec[7], clip[3]);
-       CalcNormFloat(bb->vec[4], bb->vec[5], bb->vec[6], clip[4]);
-       CalcNormFloat(bb->vec[0], bb->vec[2], bb->vec[1], clip[5]);
+       normal_tri_v3( clip[0],bb->vec[0], bb->vec[1], bb->vec[4]);
+       normal_tri_v3( clip[1],bb->vec[1], bb->vec[2], bb->vec[5]);
+       normal_tri_v3( clip[2],bb->vec[2], bb->vec[3], bb->vec[6]);
+       normal_tri_v3( clip[3],bb->vec[3], bb->vec[0], bb->vec[7]);
+       normal_tri_v3( clip[4],bb->vec[4], bb->vec[5], bb->vec[6]);
+       normal_tri_v3( clip[5],bb->vec[0], bb->vec[2], bb->vec[1]);
 
        /* then plane equations */
        for(val=0; val<5; val++) {
@@ -181,22 +181,22 @@ static void view3d_boxview_sync(ScrArea *sa, ARegion *ar)
                        if(rv3dtest->viewlock) {
                                rv3dtest->dist= rv3d->dist;
 
-                               if( ELEM(rv3d->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM) ) {
-                                       if( ELEM(rv3dtest->view, V3D_VIEW_FRONT, V3D_VIEW_BACK))
+                               if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM) ) {
+                                       if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
                                                rv3dtest->ofs[0]= rv3d->ofs[0];
-                                       else if( ELEM(rv3dtest->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT))
+                                       else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
                                                rv3dtest->ofs[1]= rv3d->ofs[1];
                                }
-                               else if( ELEM(rv3d->view, V3D_VIEW_FRONT, V3D_VIEW_BACK) ) {
-                                       if( ELEM(rv3dtest->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM))
+                               else if( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK) ) {
+                                       if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
                                                rv3dtest->ofs[0]= rv3d->ofs[0];
-                                       else if( ELEM(rv3dtest->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT))
+                                       else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
                                                rv3dtest->ofs[2]= rv3d->ofs[2];
                                }
-                               else if( ELEM(rv3d->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT) ) {
-                                       if( ELEM(rv3dtest->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM))
+                               else if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT) ) {
+                                       if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
                                                rv3dtest->ofs[1]= rv3d->ofs[1];
-                                       if( ELEM(rv3dtest->view, V3D_VIEW_FRONT, V3D_VIEW_BACK))
+                                       if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
                                                rv3dtest->ofs[2]= rv3d->ofs[2];
                                }
 
@@ -275,8 +275,9 @@ static void calctrackballvec(rcti *rect, int mx, int my, float *vec)
 }
 
 
-static void viewops_data(bContext *C, wmOperator *op, wmEvent *event)
+static void viewops_data_create(bContext *C, wmOperator *op, wmEvent *event)
 {
+       static float lastofs[3] = {0,0,0};
        View3D *v3d = CTX_wm_view3d(C);
        RegionView3D *rv3d;
        ViewOpsData *vod= MEM_callocN(sizeof(ViewOpsData), "viewops data");
@@ -291,6 +292,15 @@ static void viewops_data(bContext *C, wmOperator *op, wmEvent *event)
        vod->origx= vod->oldx= event->x;
        vod->origy= vod->oldy= event->y;
        vod->origkey= event->type; /* the key that triggered the operator.  */
+       
+       if (U.uiflag & USER_ORBIT_SELECTION)
+       {
+               VECCOPY(vod->ofs, rv3d->ofs);
+               /* If there's no selection, lastofs is unmodified and last value since static */
+               calculateTransformCenter(C, event, V3D_CENTROID, lastofs);
+               VECCOPY(vod->obofs, lastofs);
+               mul_v3_fl(vod->obofs, -1.0f);
+       }
 
        /* lookup, we dont pass on v3d to prevent confusement */
        vod->grid= v3d->grid;
@@ -304,6 +314,21 @@ static void viewops_data(bContext *C, wmOperator *op, wmEvent *event)
        if (rv3d->persmat[2][1] < 0.0f)
                vod->reverse= -1.0f;
 
+       rv3d->rflag |= RV3D_NAVIGATING;
+}
+
+static void viewops_data_free(bContext *C, wmOperator *op)
+{
+       Paint *p = paint_get_active(CTX_data_scene(C));
+       ViewOpsData *vod= op->customdata;
+
+       vod->rv3d->rflag &= ~RV3D_NAVIGATING;
+
+       if(p && (p->flags & PAINT_FAST_NAVIGATE))
+               ED_region_tag_redraw(vod->ar);
+
+       MEM_freeN(vod);
+       op->customdata= NULL;
 }
 
 /* ************************** viewrotate **********************************/
@@ -315,12 +340,12 @@ static const float thres = 0.93f; //cos(20 deg);
 
 static float snapquats[39][6] = {
        /*{q0, q1, q3, q4, view, oposite_direction}*/
-{COS45, -SIN45, 0.0, 0.0, V3D_VIEW_FRONT, 0},  //front
-{0.0, 0.0, -SIN45, -SIN45, V3D_VIEW_BACK, 0}, //back
-{1.0, 0.0, 0.0, 0.0, V3D_VIEW_TOP, 0},       //top
-{0.0, -1.0, 0.0, 0.0, V3D_VIEW_BOTTOM, 0},      //bottom
-{0.5, -0.5, -0.5, -0.5, V3D_VIEW_LEFT, 0},    //left
-{0.5, -0.5, 0.5, 0.5, V3D_VIEW_RIGHT, 0},      //right
+{COS45, -SIN45, 0.0, 0.0, RV3D_VIEW_FRONT, 0},  //front
+{0.0, 0.0, -SIN45, -SIN45, RV3D_VIEW_BACK, 0}, //back
+{1.0, 0.0, 0.0, 0.0, RV3D_VIEW_TOP, 0},       //top
+{0.0, -1.0, 0.0, 0.0, RV3D_VIEW_BOTTOM, 0},      //bottom
+{0.5, -0.5, -0.5, -0.5, RV3D_VIEW_LEFT, 0},    //left
+{0.5, -0.5, 0.5, 0.5, RV3D_VIEW_RIGHT, 0},      //right
 
        /* some more 45 deg snaps */
 {0.65328145027160645, -0.65328145027160645, 0.27059805393218994, 0.27059805393218994, 0, 0},
@@ -412,13 +437,13 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
 
                calctrackballvec(&vod->ar->winrct, x, y, newvec);
 
-               VecSubf(dvec, newvec, vod->trackvec);
+               sub_v3_v3v3(dvec, newvec, vod->trackvec);
 
                si= sqrt(dvec[0]*dvec[0]+ dvec[1]*dvec[1]+ dvec[2]*dvec[2]);
                si/= (2.0*TRACKBALLSIZE);
 
-               Crossf(q1+1, vod->trackvec, newvec);
-               Normalize(q1+1);
+               cross_v3_v3v3(q1+1, vod->trackvec, newvec);
+               normalize_v3(q1+1);
 
                /* Allow for rotation beyond the interval
                        * [-pi, pi] */
@@ -437,19 +462,19 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
                q1[1]*= si;
                q1[2]*= si;
                q1[3]*= si;
-               QuatMul(rv3d->viewquat, q1, vod->oldquat);
+               mul_qt_qtqt(rv3d->viewquat, q1, vod->oldquat);
 
                if (use_sel) {
                        /* compute the post multiplication quat, to rotate the offset correctly */
                        QUATCOPY(q1, vod->oldquat);
-                       QuatConj(q1);
-                       QuatMul(q1, q1, rv3d->viewquat);
+                       conjugate_qt(q1);
+                       mul_qt_qtqt(q1, q1, rv3d->viewquat);
 
-                       QuatConj(q1); /* conj == inv for unit quat */
+                       conjugate_qt(q1); /* conj == inv for unit quat */
                        VECCOPY(rv3d->ofs, vod->ofs);
-                       VecSubf(rv3d->ofs, rv3d->ofs, vod->obofs);
-                       QuatMulVecf(q1, rv3d->ofs);
-                       VecAddf(rv3d->ofs, rv3d->ofs, vod->obofs);
+                       sub_v3_v3v3(rv3d->ofs, rv3d->ofs, vod->obofs);
+                       mul_qt_v3(q1, rv3d->ofs);
+                       add_v3_v3v3(rv3d->ofs, rv3d->ofs, vod->obofs);
                }
        }
        else {
@@ -465,12 +490,12 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
                const float sensitivity = 0.0035;
 
                /* Get the 3x3 matrix and its inverse from the quaternion */
-               QuatToMat3(rv3d->viewquat, m);
-               Mat3Inv(m_inv,m);
+               quat_to_mat3( m,rv3d->viewquat);
+               invert_m3_m3(m_inv,m);
 
                /* Determine the direction of the x vector (for rotating up and down) */
                /* This can likely be compuated directly from the quaternion. */
-               Mat3MulVecfl(m_inv,xvec);
+               mul_m3_v3(m_inv,xvec);
 
                /* Perform the up/down rotation */
                phi = sensitivity * -(y - vod->oldy);
@@ -479,13 +504,13 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
                q1[1] = si * xvec[0];
                q1[2] = si * xvec[1];
                q1[3] = si * xvec[2];
-               QuatMul(rv3d->viewquat, rv3d->viewquat, q1);
+               mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
 
                if (use_sel) {
-                       QuatConj(q1); /* conj == inv for unit quat */
-                       VecSubf(rv3d->ofs, rv3d->ofs, vod->obofs);
-                       QuatMulVecf(q1, rv3d->ofs);
-                       VecAddf(rv3d->ofs, rv3d->ofs, vod->obofs);
+                       conjugate_qt(q1); /* conj == inv for unit quat */
+                       sub_v3_v3v3(rv3d->ofs, rv3d->ofs, vod->obofs);
+                       mul_qt_v3(q1, rv3d->ofs);
+                       add_v3_v3v3(rv3d->ofs, rv3d->ofs, vod->obofs);
                }
 
                /* Perform the orbital rotation */
@@ -493,13 +518,13 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
                q1[0] = cos(phi);
                q1[1] = q1[2] = 0.0;
                q1[3] = sin(phi);
-               QuatMul(rv3d->viewquat, rv3d->viewquat, q1);
+               mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
 
                if (use_sel) {
-                       QuatConj(q1);
-                       VecSubf(rv3d->ofs, rv3d->ofs, vod->obofs);
-                       QuatMulVecf(q1, rv3d->ofs);
-                       VecAddf(rv3d->ofs, rv3d->ofs, vod->obofs);
+                       conjugate_qt(q1);
+                       sub_v3_v3v3(rv3d->ofs, rv3d->ofs, vod->obofs);
+                       mul_qt_v3(q1, rv3d->ofs);
+                       add_v3_v3v3(rv3d->ofs, rv3d->ofs, vod->obofs);
                }
        }
 
@@ -509,17 +534,17 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
                float viewmat[3][3];
 
 
-               QuatToMat3(rv3d->viewquat, viewmat);
+               quat_to_mat3( viewmat,rv3d->viewquat);
 
                for (i = 0 ; i < 39; i++){
                        float snapmat[3][3];
                        float view = (int)snapquats[i][4];
 
-                       QuatToMat3(snapquats[i], snapmat);
+                       quat_to_mat3( snapmat,snapquats[i]);
 
-                       if ((Inpf(snapmat[0], viewmat[0]) > thres) &&
-                               (Inpf(snapmat[1], viewmat[1]) > thres) &&
-                               (Inpf(snapmat[2], viewmat[2]) > thres)){
+                       if ((dot_v3v3(snapmat[0], viewmat[0]) > thres) &&
+                               (dot_v3v3(snapmat[1], viewmat[1]) > thres) &&
+                               (dot_v3v3(snapmat[2], viewmat[2]) > thres)){
 
                                QUATCOPY(rv3d->viewquat, snapquats[i]);
 
@@ -568,9 +593,7 @@ static int viewrotate_modal(bContext *C, wmOperator *op, wmEvent *event)
        }
        else if (event_code==VIEW_CONFIRM) {
                request_depth_update(CTX_wm_region_view3d(C));
-
-               MEM_freeN(vod);
-               op->customdata= NULL;
+               viewops_data_free(C, op);
 
                return OPERATOR_FINISHED;
        }
@@ -587,16 +610,16 @@ static int viewrotate_invoke(bContext *C, wmOperator *op, wmEvent *event)
                return OPERATOR_CANCELLED;
 
        /* makes op->customdata */
-       viewops_data(C, op, event);
+       viewops_data_create(C, op, event);
        vod= op->customdata;
 
        /* switch from camera view when: */
-       if(vod->rv3d->persp != V3D_PERSP) {
+       if(vod->rv3d->persp != RV3D_PERSP) {
 
                if (U.uiflag & USER_AUTOPERSP)
-                       vod->rv3d->persp= V3D_PERSP;
-               else if(vod->rv3d->persp==V3D_CAMOB)
-                       vod->rv3d->persp= V3D_PERSP;
+                       vod->rv3d->persp= RV3D_PERSP;
+               else if(vod->rv3d->persp==RV3D_CAMOB)
+                       vod->rv3d->persp= RV3D_PERSP;
                ED_region_tag_redraw(vod->ar);
        }
 
@@ -655,7 +678,7 @@ void viewmove_modal_keymap(wmKeyConfig *keyconf)
 
 static void viewmove_apply(ViewOpsData *vod, int x, int y)
 {
-       if(vod->rv3d->persp==V3D_CAMOB) {
+       if(vod->rv3d->persp==RV3D_CAMOB) {
                float max= (float)MAX2(vod->ar->winx, vod->ar->winy);
 
                vod->rv3d->camdx += (vod->oldx - x)/(max);
@@ -668,7 +691,7 @@ static void viewmove_apply(ViewOpsData *vod, int x, int y)
                float dvec[3];
 
                window_to_3d_delta(vod->ar, dvec, x-vod->oldx, y-vod->oldy);
-               VecAddf(vod->rv3d->ofs, vod->rv3d->ofs, dvec);
+               add_v3_v3v3(vod->rv3d->ofs, vod->rv3d->ofs, dvec);
 
                if(vod->rv3d->viewlock & RV3D_BOXVIEW)
                        view3d_boxview_sync(vod->sa, vod->ar);
@@ -708,8 +731,7 @@ static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
        else if (event_code==VIEW_CONFIRM) {
                request_depth_update(CTX_wm_region_view3d(C));
 
-               MEM_freeN(vod);
-               op->customdata= NULL;
+               viewops_data_free(C, op);
 
                return OPERATOR_FINISHED;
        }
@@ -720,7 +742,7 @@ static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
 static int viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        /* makes op->customdata */
-       viewops_data(C, op, event);
+       viewops_data_create(C, op, event);
 
        /* add temp handler */
        WM_event_add_modal_handler(C, op);
@@ -851,23 +873,23 @@ static void viewzoom_apply(ViewOpsData *vod, int x, int y)
                view_zoom_mouseloc(vod->ar, zfac, vod->oldx, vod->oldy);
 
 
-       if ((U.uiflag & USER_ORBIT_ZBUF) && (U.viewzoom==USER_ZOOM_CONT) && (vod->rv3d->persp==V3D_PERSP)) {
+       if ((U.uiflag & USER_ORBIT_ZBUF) && (U.viewzoom==USER_ZOOM_CONT) && (vod->rv3d->persp==RV3D_PERSP)) {
                float upvec[3], mat[3][3];
 
                /* Secret apricot feature, translate the view when in continues mode */
                upvec[0] = upvec[1] = 0.0f;
                upvec[2] = (vod->dist0 - vod->rv3d->dist) * vod->grid;
                vod->rv3d->dist = vod->dist0;
-               Mat3CpyMat4(mat, vod->rv3d->viewinv);
-               Mat3MulVecfl(mat, upvec);
-               VecAddf(vod->rv3d->ofs, vod->rv3d->ofs, upvec);
+               copy_m3_m4(mat, vod->rv3d->viewinv);
+               mul_m3_v3(mat, upvec);
+               add_v3_v3v3(vod->rv3d->ofs, vod->rv3d->ofs, upvec);
        } else {
                /* these limits were in old code too */
                if(vod->rv3d->dist<0.001*vod->grid) vod->rv3d->dist= 0.001*vod->grid;
                if(vod->rv3d->dist>10.0*vod->far) vod->rv3d->dist=10.0*vod->far;
        }
 
-// XXX if(vod->rv3d->persp==V3D_ORTHO || vod->rv3d->persp==V3D_CAMOB) preview3d_event= 0;
+// XXX if(vod->rv3d->persp==RV3D_ORTHO || vod->rv3d->persp==RV3D_CAMOB) preview3d_event= 0;
 
        if(vod->rv3d->viewlock & RV3D_BOXVIEW)
                view3d_boxview_sync(vod->sa, vod->ar);
@@ -901,9 +923,7 @@ static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event)
        }
        else if (event_code==VIEW_CONFIRM) {
                request_depth_update(CTX_wm_region_view3d(C));
-
-               MEM_freeN(vod);
-               op->customdata= NULL;
+               viewops_data_free(C, op);
 
                return OPERATOR_FINISHED;
        }
@@ -916,21 +936,27 @@ static int viewzoom_exec(bContext *C, wmOperator *op)
        View3D *v3d = CTX_wm_view3d(C);
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
        int delta= RNA_int_get(op->ptr, "delta");
+       int mx = RNA_int_get(op->ptr, "mx");
+       int my = RNA_int_get(op->ptr, "my");
 
        if(delta < 0) {
                /* this min and max is also in viewmove() */
-               if(rv3d->persp==V3D_CAMOB) {
+               if(rv3d->persp==RV3D_CAMOB) {
                        rv3d->camzoom-= 10;
                        if(rv3d->camzoom<-30) rv3d->camzoom= -30;
                }
-               else if(rv3d->dist<10.0*v3d->far) rv3d->dist*=1.2f;
+               else if(rv3d->dist<10.0*v3d->far) {
+                       view_zoom_mouseloc(CTX_wm_region(C), 1.2f, mx, my);
+               }
        }
        else {
-               if(rv3d->persp==V3D_CAMOB) {
+               if(rv3d->persp==RV3D_CAMOB) {
                        rv3d->camzoom+= 10;
                        if(rv3d->camzoom>300) rv3d->camzoom= 300;
                }
-               else if(rv3d->dist> 0.001*v3d->grid) rv3d->dist*=.83333f;
+               else if(rv3d->dist> 0.001*v3d->grid) {
+                       view_zoom_mouseloc(CTX_wm_region(C), .83333f, mx, my);
+               }
        }
 
        if(rv3d->viewlock & RV3D_BOXVIEW)
@@ -945,13 +971,20 @@ static int viewzoom_exec(bContext *C, wmOperator *op)
 static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        int delta= RNA_int_get(op->ptr, "delta");
+       
+       /* if one or the other zoom position aren't set, set from event */
+       if (!RNA_property_is_set(op->ptr, "mx") || !RNA_property_is_set(op->ptr, "my"))
+       {
+               RNA_int_set(op->ptr, "mx", event->x);
+               RNA_int_set(op->ptr, "my", event->y);
+       }
 
        if(delta) {
                viewzoom_exec(C, op);
        }
        else {
                /* makes op->customdata */
-               viewops_data(C, op, event);
+               viewops_data_create(C, op, event);
 
                /* add temp handler */
                WM_event_add_modal_handler(C, op);
@@ -979,6 +1012,8 @@ void VIEW3D_OT_zoom(wmOperatorType *ot)
        ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
 
        RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
+       RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
+       RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
 }
 
 static int viewhome_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
@@ -1037,8 +1072,8 @@ static int viewhome_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.
                        new_dist*= size;
                }
 
-               if (rv3d->persp==V3D_CAMOB) {
-                       rv3d->persp= V3D_PERSP;
+               if (rv3d->persp==RV3D_CAMOB) {
+                       rv3d->persp= RV3D_PERSP;
                        smooth_view(C, NULL, v3d->camera, new_ofs, NULL, &new_dist, NULL);
                }
         else {
@@ -1117,10 +1152,10 @@ static int viewcenter_exec(bContext *C, wmOperator *op) /* like a localview with
                                        if(pchan->bone->layer & arm->layer) {
                                                ok= 1;
                                                VECCOPY(vec, pchan->pose_head);
-                                               Mat4MulVecfl(ob->obmat, vec);
+                                               mul_m4_v3(ob->obmat, vec);
                                                DO_MINMAX(vec, min, max);
                                                VECCOPY(vec, pchan->pose_tail);
-                                               Mat4MulVecfl(ob->obmat, vec);
+                                               mul_m4_v3(ob->obmat, vec);
                                                DO_MINMAX(vec, min, max);
                                        }
                                }
@@ -1154,7 +1189,7 @@ static int viewcenter_exec(bContext *C, wmOperator *op) /* like a localview with
        afm[2]= (max[2]-min[2]);
        size= MAX3(afm[0], afm[1], afm[2]);
        /* perspective should be a bit farther away to look nice */
-       if(rv3d->persp==V3D_ORTHO)
+       if(rv3d->persp==RV3D_ORTHO)
                size*= 0.7;
 
        if(size <= v3d->near*1.5f) size= v3d->near*1.5f;
@@ -1176,8 +1211,8 @@ static int viewcenter_exec(bContext *C, wmOperator *op) /* like a localview with
        v3d->cursor[1]= -new_ofs[1];
        v3d->cursor[2]= -new_ofs[2];
 
-       if (rv3d->persp==V3D_CAMOB) {
-               rv3d->persp= V3D_PERSP;
+       if (rv3d->persp==RV3D_CAMOB) {
+               rv3d->persp= RV3D_PERSP;
                smooth_view(C, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
        }
        else {
@@ -1207,6 +1242,51 @@ void VIEW3D_OT_view_center(wmOperatorType *ot)
        ot->flag= 0;
 }
 
+static int viewcenter_cursor_exec(bContext *C, wmOperator *op)
+{
+       View3D *v3d = CTX_wm_view3d(C);
+       RegionView3D *rv3d= CTX_wm_region_view3d(C);
+       Scene *scene= CTX_data_scene(C);
+       
+       if (rv3d) {
+               if (rv3d->persp==RV3D_CAMOB) {
+                       /* center the camera offset */
+                       rv3d->camdx= rv3d->camdy= 0.0;
+               }
+               else {
+                       /* non camera center */
+                       float *curs= give_cursor(scene, v3d);
+                       float new_ofs[3];
+                       
+                       new_ofs[0]= -curs[0];
+                       new_ofs[1]= -curs[1];
+                       new_ofs[2]= -curs[2];
+                       
+                       smooth_view(C, NULL, NULL, new_ofs, NULL, NULL, NULL);
+               }
+               
+               if (rv3d->viewlock & RV3D_BOXVIEW)
+                       view3d_boxview_copy(CTX_wm_area(C), CTX_wm_region(C));
+       }
+       
+       return OPERATOR_FINISHED;
+}
+
+void VIEW3D_OT_view_center_cursor(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Center View to Cursor";
+       ot->description= "Centers the view so that the cursor is in the middle of the view.";
+       ot->idname= "VIEW3D_OT_view_center_cursor";
+       
+       /* api callbacks */
+       ot->exec= viewcenter_cursor_exec;
+       ot->poll= ED_operator_view3d_active;
+       
+       /* flags */
+       ot->flag= 0;
+}
+
 /* ********************* Set render border operator ****************** */
 
 static int render_border_exec(bContext *C, wmOperator *op)
@@ -1259,7 +1339,7 @@ static int view3d_render_border_invoke(bContext *C, wmOperator *op, wmEvent *eve
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
 
        /* if not in camera view do not exec the operator*/
-       if (rv3d->persp == V3D_CAMOB) return WM_border_select_invoke(C, op, event);
+       if (rv3d->persp == RV3D_CAMOB) return WM_border_select_invoke(C, op, event);
        else return OPERATOR_PASS_THROUGH;
 }
 
@@ -1359,7 +1439,7 @@ static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
        cent[0] = (((double)rect.xmin)+((double)rect.xmax)) / 2;
        cent[1] = (((double)rect.ymin)+((double)rect.ymax)) / 2;
 
-       if (rv3d->persp==V3D_PERSP) {
+       if (rv3d->persp==RV3D_PERSP) {
                double p_corner[3];
 
                /* no depths to use, we cant do anything! */
@@ -1376,7 +1456,7 @@ static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
                dvec[1] = p[1]-p_corner[1];
                dvec[2] = p[2]-p_corner[2];
 
-               new_dist = VecLength(dvec);
+               new_dist = len_v3(dvec);
                if(new_dist <= v3d->near*1.5) new_dist= v3d->near*1.5;
 
                new_ofs[0] = -p[0];
@@ -1405,7 +1485,7 @@ static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
 
                        window_to_3d_delta(ar, dvec, (rect.xmin+rect.xmax-vb[0])/2, (rect.ymin+rect.ymax-vb[1])/2);
                        /* center the view to the center of the rectangle */
-                       VecSubf(new_ofs, new_ofs, dvec);
+                       sub_v3_v3v3(new_ofs, new_ofs, dvec);
                }
 
                /* work out the ratios, so that everything selected fits when we zoom */
@@ -1430,7 +1510,7 @@ static int view3d_zoom_border_invoke(bContext *C, wmOperator *op, wmEvent *event
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
 
        /* if in camera view do not exec the operator so we do not conflict with set render border*/
-       if (rv3d->persp != V3D_CAMOB)
+       if (rv3d->persp != RV3D_CAMOB)
                return WM_border_select_invoke(C, op, event);
        else
                return OPERATOR_PASS_THROUGH;
@@ -1464,53 +1544,81 @@ void VIEW3D_OT_zoom_border(wmOperatorType *ot)
 /* ********************* Changing view operator ****************** */
 
 static EnumPropertyItem prop_view_items[] = {
-       {V3D_VIEW_FRONT, "FRONT", 0, "Front", "View From the Front"},
-       {V3D_VIEW_BACK, "BACK", 0, "Back", "View From the Back"},
-       {V3D_VIEW_LEFT, "LEFT", 0, "Left", "View From the Left"},
-       {V3D_VIEW_RIGHT, "RIGHT", 0, "Right", "View From the Right"},
-       {V3D_VIEW_TOP, "TOP", 0, "Top", "View From the Top"},
-       {V3D_VIEW_BOTTOM, "BOTTOM", 0, "Bottom", "View From the Bottom"},
-       {V3D_VIEW_CAMERA, "CAMERA", 0, "Camera", "View From the active amera"},
+       {RV3D_VIEW_FRONT, "FRONT", 0, "Front", "View From the Front"},
+       {RV3D_VIEW_BACK, "BACK", 0, "Back", "View From the Back"},
+       {RV3D_VIEW_LEFT, "LEFT", 0, "Left", "View From the Left"},
+       {RV3D_VIEW_RIGHT, "RIGHT", 0, "Right", "View From the Right"},
+       {RV3D_VIEW_TOP, "TOP", 0, "Top", "View From the Top"},
+       {RV3D_VIEW_BOTTOM, "BOTTOM", 0, "Bottom", "View From the Bottom"},
+       {RV3D_VIEW_CAMERA, "CAMERA", 0, "Camera", "View From the active amera"},
        {0, NULL, 0, NULL, NULL}};
 
-static void axis_set_view(bContext *C, float q1, float q2, float q3, float q4, short view, int perspo)
+
+/* would like to make this a generic function - outside of transform */
+
+static void axis_set_view(bContext *C, float q1, float q2, float q3, float q4, short view, int perspo, int align_active)
 {
        View3D *v3d = CTX_wm_view3d(C);
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
        float new_quat[4];
 
-       if(rv3d->viewlock) {
-               /* only pass on if */
-               if(rv3d->view==V3D_VIEW_FRONT && view==V3D_VIEW_BACK);
-               else if(rv3d->view==V3D_VIEW_BACK && view==V3D_VIEW_FRONT);
-               else if(rv3d->view==V3D_VIEW_RIGHT && view==V3D_VIEW_LEFT);
-               else if(rv3d->view==V3D_VIEW_LEFT && view==V3D_VIEW_RIGHT);
-               else if(rv3d->view==V3D_VIEW_BOTTOM && view==V3D_VIEW_TOP);
-               else if(rv3d->view==V3D_VIEW_TOP && view==V3D_VIEW_BOTTOM);
-               else return;
-       }
-
        new_quat[0]= q1; new_quat[1]= q2;
        new_quat[2]= q3; new_quat[3]= q4;
 
-       rv3d->view= view;
+       if(align_active) {
+               /* align to active object */
+               Object *obact= CTX_data_active_object(C);
+               if (obact==NULL) {
+                       /* no active object, ignore this option */
+                       align_active= FALSE;
+               }
+               else {
+                       float obact_quat[4];
+                       float twmat[3][3];
+
+                       /* same as transform manipulator when normal is set */
+                       ED_getTransformOrientationMatrix(C, twmat, TRUE);
+
+                       mat3_to_quat( obact_quat,twmat);
+                       invert_qt(obact_quat);
+                       mul_qt_qtqt(new_quat, new_quat, obact_quat);
+
+                       rv3d->view= view= 0;
+               }
+       }
+
+       if(align_active==FALSE) {
+               /* normal operation */
+               if(rv3d->viewlock) {
+                       /* only pass on if */
+                       if(rv3d->view==RV3D_VIEW_FRONT && view==RV3D_VIEW_BACK);
+                       else if(rv3d->view==RV3D_VIEW_BACK && view==RV3D_VIEW_FRONT);
+                       else if(rv3d->view==RV3D_VIEW_RIGHT && view==RV3D_VIEW_LEFT);
+                       else if(rv3d->view==RV3D_VIEW_LEFT && view==RV3D_VIEW_RIGHT);
+                       else if(rv3d->view==RV3D_VIEW_BOTTOM && view==RV3D_VIEW_TOP);
+                       else if(rv3d->view==RV3D_VIEW_TOP && view==RV3D_VIEW_BOTTOM);
+                       else return;
+               }
+
+               rv3d->view= view;
+       }
 
        if(rv3d->viewlock) {
                ED_region_tag_redraw(CTX_wm_region(C));
                return;
        }
 
-       if (rv3d->persp==V3D_CAMOB && v3d->camera) {
+       if (rv3d->persp==RV3D_CAMOB && v3d->camera) {
 
-               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= V3D_ORTHO;
-               else if(rv3d->persp==V3D_CAMOB) rv3d->persp= perspo;
+               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= RV3D_ORTHO;
+               else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
 
                smooth_view(C, v3d->camera, NULL, rv3d->ofs, new_quat, NULL, NULL);
        }
        else {
 
-               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= V3D_ORTHO;
-               else if(rv3d->persp==V3D_CAMOB) rv3d->persp= perspo;
+               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= RV3D_ORTHO;
+               else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
 
                smooth_view(C, NULL, NULL, NULL, new_quat, NULL, NULL);
        }
@@ -1522,43 +1630,44 @@ static int viewnumpad_exec(bContext *C, wmOperator *op)
        View3D *v3d = CTX_wm_view3d(C);
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
        Scene *scene= CTX_data_scene(C);
-       static int perspo=V3D_PERSP;
-       int viewnum;
+       static int perspo=RV3D_PERSP;
+       int viewnum, align_active;
 
        viewnum = RNA_enum_get(op->ptr, "type");
+       align_active = RNA_boolean_get(op->ptr, "align_active");
 
        /* Use this to test if we started out with a camera */
 
        switch (viewnum) {
-               case V3D_VIEW_BOTTOM :
-                       axis_set_view(C, 0.0, -1.0, 0.0, 0.0, viewnum, perspo);
+               case RV3D_VIEW_BOTTOM :
+                       axis_set_view(C, 0.0, -1.0, 0.0, 0.0, viewnum, perspo, align_active);
                        break;
 
-               case V3D_VIEW_BACK:
-                       axis_set_view(C, 0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0), viewnum, perspo);
+               case RV3D_VIEW_BACK:
+                       axis_set_view(C, 0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0), viewnum, perspo, align_active);
                        break;
 
-               case V3D_VIEW_LEFT:
-                       axis_set_view(C, 0.5, -0.5, 0.5, 0.5, viewnum, perspo);
+               case RV3D_VIEW_LEFT:
+                       axis_set_view(C, 0.5, -0.5, 0.5, 0.5, viewnum, perspo, align_active);
                        break;
 
-               case V3D_VIEW_TOP:
-                       axis_set_view(C, 1.0, 0.0, 0.0, 0.0, viewnum, perspo);
+               case RV3D_VIEW_TOP:
+                       axis_set_view(C, 1.0, 0.0, 0.0, 0.0, viewnum, perspo, align_active);
                        break;
 
-               case V3D_VIEW_FRONT:
-                       axis_set_view(C, (float)cos(M_PI/4.0), (float)-sin(M_PI/4.0), 0.0, 0.0, viewnum, perspo);
+               case RV3D_VIEW_FRONT:
+                       axis_set_view(C, (float)cos(M_PI/4.0), (float)-sin(M_PI/4.0), 0.0, 0.0, viewnum, perspo, align_active);
                        break;
 
-               case V3D_VIEW_RIGHT:
-                       axis_set_view(C, 0.5, -0.5, -0.5, -0.5, viewnum, perspo);
+               case RV3D_VIEW_RIGHT:
+                       axis_set_view(C, 0.5, -0.5, -0.5, -0.5, viewnum, perspo, align_active);
                        break;
 
-               case V3D_VIEW_CAMERA:
+               case RV3D_VIEW_CAMERA:
                        if(rv3d->viewlock==0) {
                                /* lastview -  */
 
-                               if(rv3d->persp != V3D_CAMOB) {
+                               if(rv3d->persp != RV3D_CAMOB) {
                                        /* store settings of current view before allowing overwriting with camera view */
                                        QUATCOPY(rv3d->lviewquat, rv3d->viewquat);
                                        rv3d->lview= rv3d->view;
@@ -1585,14 +1694,14 @@ static int viewnumpad_exec(bContext *C, wmOperator *op)
                                                v3d->camera= scene_find_camera(scene);
                                                /*handle_view3d_lock();*/
                                        }
-                                       rv3d->persp= V3D_CAMOB;
+                                       rv3d->persp= RV3D_CAMOB;
                                        smooth_view(C, NULL, v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens);
 
                                }
                                else{
                                        /* return to settings of last view */
                                        /* does smooth_view too */
-                                       axis_set_view(C, rv3d->lviewquat[0], rv3d->lviewquat[1], rv3d->lviewquat[2], rv3d->lviewquat[3], rv3d->lview, rv3d->lpersp);
+                                       axis_set_view(C, rv3d->lviewquat[0], rv3d->lviewquat[1], rv3d->lviewquat[2], rv3d->lviewquat[3], rv3d->lview, rv3d->lpersp, 0);
                                }
                        }
                        break;
@@ -1601,7 +1710,7 @@ static int viewnumpad_exec(bContext *C, wmOperator *op)
                        break;
        }
 
-       if(rv3d->persp != V3D_CAMOB) perspo= rv3d->persp;
+       if(rv3d->persp != RV3D_CAMOB) perspo= rv3d->persp;
 
        return OPERATOR_FINISHED;
 }
@@ -1620,6 +1729,7 @@ void VIEW3D_OT_viewnumpad(wmOperatorType *ot)
        ot->flag= 0;
 
        RNA_def_enum(ot->srna, "type", prop_view_items, 0, "View", "The Type of view");
+       RNA_def_boolean(ot->srna, "align_active", 0, "Align Active", "Align to the active objects axis");
 }
 
 static EnumPropertyItem prop_view_orbit_items[] = {
@@ -1640,7 +1750,7 @@ static int vieworbit_exec(bContext *C, wmOperator *op)
 
        if(rv3d->viewlock==0) {
 
-               if(rv3d->persp != V3D_CAMOB) {
+               if(rv3d->persp != RV3D_CAMOB) {
                        if(orbitdir == V3D_VIEW_STEPLEFT || orbitdir == V3D_VIEW_STEPRIGHT) {
                                /* z-axis */
                                phi= (float)(M_PI/360.0)*U.pad_rot_angle;
@@ -1649,14 +1759,14 @@ static int vieworbit_exec(bContext *C, wmOperator *op)
                                q1[0]= (float)cos(phi);
                                q1[1]= q1[2]= 0.0;
                                q1[3]= si;
-                               QuatMul(rv3d->viewquat, rv3d->viewquat, q1);
+                               mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
                                rv3d->view= 0;
                        }
                        if(orbitdir == V3D_VIEW_STEPDOWN || orbitdir == V3D_VIEW_STEPUP) {
                                /* horizontal axis */
                                VECCOPY(q1+1, rv3d->viewinv[0]);
 
-                               Normalize(q1+1);
+                               normalize_v3(q1+1);
                                phi= (float)(M_PI/360.0)*U.pad_rot_angle;
                                if(orbitdir == V3D_VIEW_STEPDOWN) phi= -phi;
                                si= (float)sin(phi);
@@ -1664,7 +1774,7 @@ static int vieworbit_exec(bContext *C, wmOperator *op)
                                q1[1]*= si;
                                q1[2]*= si;
                                q1[3]*= si;
-                               QuatMul(rv3d->viewquat, rv3d->viewquat, q1);
+                               mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
                                rv3d->view= 0;
                        }
                        ED_region_tag_redraw(ar);
@@ -1746,9 +1856,9 @@ static int viewpersportho_exec(bContext *C, wmOperator *op)
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
 
        if(rv3d->viewlock==0) {
-               if(rv3d->persp!=V3D_ORTHO)
-                       rv3d->persp=V3D_ORTHO;
-               else rv3d->persp=V3D_PERSP;
+               if(rv3d->persp!=RV3D_ORTHO)
+                       rv3d->persp=RV3D_ORTHO;
+               else rv3d->persp=RV3D_PERSP;
                ED_region_tag_redraw(ar);
        }
 
@@ -1774,15 +1884,48 @@ void VIEW3D_OT_view_persportho(wmOperatorType *ot)
 
 /* ********************* set clipping operator ****************** */
 
+static void calc_clipping_plane(float clip[6][4], BoundBox *clipbb)
+{
+       int val;
+
+       for(val=0; val<4; val++) {
+
+               normal_tri_v3( clip[val],clipbb->vec[val], clipbb->vec[val==3?0:val+1], clipbb->vec[val+4]);
+
+               clip[val][3]=
+                       - clip[val][0]*clipbb->vec[val][0]
+                       - clip[val][1]*clipbb->vec[val][1]
+                       - clip[val][2]*clipbb->vec[val][2];
+       }
+}
+
+static void calc_local_clipping(float clip_local[][4], BoundBox *clipbb, float mat[][4])
+{
+       BoundBox clipbb_local;
+       float imat[4][4];
+       int i;
+
+       invert_m4_m4(imat, mat);
+
+       for(i=0; i<8; i++) {
+               mul_v3_m4v3(clipbb_local.vec[i], imat, clipbb->vec[i]);
+       }
+
+       calc_clipping_plane(clip_local, &clipbb_local);
+}
+
+void ED_view3d_local_clipping(RegionView3D *rv3d, float mat[][4])
+{
+       if(rv3d->rflag & RV3D_CLIPPING)
+               calc_local_clipping(rv3d->clip_local, rv3d->clipbb, mat);
+}
+
 static int view3d_clipping_exec(bContext *C, wmOperator *op)
 {
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
+       ViewContext vc;
+       bglMats mats;
        rcti rect;
-       double mvmatrix[16];
-       double projmatrix[16];
-       double xs, ys, p[3];
-       GLint viewport[4];
-       short val;
 
        rect.xmin= RNA_int_get(op->ptr, "xmin");
        rect.ymin= RNA_int_get(op->ptr, "ymin");
@@ -1795,44 +1938,10 @@ static int view3d_clipping_exec(bContext *C, wmOperator *op)
        /* note; otherwise opengl won't work */
        view3d_operator_needs_opengl(C);
 
-       /* Get the matrices needed for gluUnProject */
-       glGetIntegerv(GL_VIEWPORT, viewport);
-       glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
-       glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
-
-       /* near zero floating point values can give issues with gluUnProject
-               in side view on some implementations */
-       if(fabs(mvmatrix[0]) < 1e-6) mvmatrix[0]= 0.0;
-       if(fabs(mvmatrix[5]) < 1e-6) mvmatrix[5]= 0.0;
-
-       /* Set up viewport so that gluUnProject will give correct values */
-       viewport[0] = 0;
-       viewport[1] = 0;
+       view3d_set_viewcontext(C, &vc);
+       view3d_get_transformation(vc.ar, vc.rv3d, vc.obact, &mats);
+       view3d_calculate_clipping(rv3d->clipbb, rv3d->clip, &mats, &rect);
 
-       /* four clipping planes and bounding volume */
-       /* first do the bounding volume */
-       for(val=0; val<4; val++) {
-
-               xs= (val==0||val==3)?rect.xmin:rect.xmax;
-               ys= (val==0||val==1)?rect.ymin:rect.ymax;
-
-               gluUnProject(xs, ys, 0.0, mvmatrix, projmatrix, viewport, &p[0], &p[1], &p[2]);
-               VECCOPY(rv3d->clipbb->vec[val], p);
-
-               gluUnProject(xs, ys, 1.0, mvmatrix, projmatrix, viewport, &p[0], &p[1], &p[2]);
-               VECCOPY(rv3d->clipbb->vec[4+val], p);
-       }
-
-       /* then plane equations */
-       for(val=0; val<4; val++) {
-
-               CalcNormFloat(rv3d->clipbb->vec[val], rv3d->clipbb->vec[val==3?0:val+1], rv3d->clipbb->vec[val+4],
-                                         rv3d->clip[val]);
-
-               rv3d->clip[val][3]= - rv3d->clip[val][0]*rv3d->clipbb->vec[val][0]
-                       - rv3d->clip[val][1]*rv3d->clipbb->vec[val][1]
-                       - rv3d->clip[val][2]*rv3d->clipbb->vec[val][2];
-       }
        return OPERATOR_FINISHED;
 }
 
@@ -1906,7 +2015,7 @@ static int set_3dcursor_invoke(bContext *C, wmOperator *op, wmEvent *event)
        if(mval[0]!=IS_CLIPPED) {
 
                window_to_3d_delta(ar, dvec, mval[0]-mx, mval[1]-my);
-               VecSubf(fp, fp, dvec);
+               sub_v3_v3v3(fp, fp, dvec);
        }
        else {
 
@@ -1945,7 +2054,10 @@ void VIEW3D_OT_cursor3d(wmOperatorType *ot)
        ot->invoke= set_3dcursor_invoke;
 
        ot->poll= ED_operator_view3d_active;
-
+    
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+    
        /* rna later */
 
 }
@@ -1960,6 +2072,9 @@ static int manipulator_invoke(bContext *C, wmOperator *op, wmEvent *event)
        if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return OPERATOR_PASS_THROUGH;
        if(!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return OPERATOR_PASS_THROUGH;
 
+       /* only no modifier or shift */
+       if(event->keymodifier != 0 && event->keymodifier != KM_SHIFT) return OPERATOR_PASS_THROUGH;
+
        /* note; otherwise opengl won't work */
        view3d_operator_needs_opengl(C);
 
@@ -2170,7 +2285,7 @@ void viewmoveNDOFfly(ARegion *ar, View3D *v3d, int mode)
        // until the first draw and doesn't update the menu
        // to reflect persp mode.
 
-       rv3d->persp = V3D_PERSP;
+       rv3d->persp = RV3D_PERSP;
 
 
        // Correct the distance jump if rv3d->dist != 0
@@ -2190,9 +2305,9 @@ void viewmoveNDOFfly(ARegion *ar, View3D *v3d, int mode)
                m_dist = rv3d->dist;
                upvec[0] = upvec[1] = 0;
                upvec[2] = rv3d->dist;
-               Mat3CpyMat4(mat, rv3d->viewinv);
-               Mat3MulVecfl(mat, upvec);
-               VecSubf(rv3d->ofs, rv3d->ofs, upvec);
+               copy_m3_m4(mat, rv3d->viewinv);
+               mul_m3_v3(mat, upvec);
+               sub_v3_v3v3(rv3d->ofs, rv3d->ofs, upvec);
                rv3d->dist = 0.0;
        }
 
@@ -2206,16 +2321,16 @@ void viewmoveNDOFfly(ARegion *ar, View3D *v3d, int mode)
 
        // rotate device x and y by view z
 
-       Mat3CpyMat4(mat, rv3d->viewinv);
+       copy_m3_m4(mat, rv3d->viewinv);
        mat[2][2] = 0.0f;
-       Mat3MulVecfl(mat, rvec);
+       mul_m3_v3(mat, rvec);
 
        // rotate the view
 
-       phi = Normalize(rvec);
+       phi = normalize_v3(rvec);
        if(phi != 0) {
-               VecRotToQuat(rvec,phi,q1);
-               QuatMul(rv3d->viewquat, rv3d->viewquat, q1);
+               axis_angle_to_quat(q1,rvec,phi);
+               mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
        }
 
 
@@ -2228,13 +2343,13 @@ void viewmoveNDOFfly(ARegion *ar, View3D *v3d, int mode)
        // the next three lines rotate the x and y translation coordinates
        // by the current z axis angle
 
-       Mat3CpyMat4(mat, rv3d->viewinv);
+       copy_m3_m4(mat, rv3d->viewinv);
        mat[2][2] = 0.0f;
-       Mat3MulVecfl(mat, tvec);
+       mul_m3_v3(mat, tvec);
 
        // translate the view
 
-       VecSubf(rv3d->ofs, rv3d->ofs, tvec);
+       sub_v3_v3v3(rv3d->ofs, rv3d->ofs, tvec);
 
 
        /*----------------------------------------------------
@@ -2293,9 +2408,9 @@ void viewmoveNDOF(Scene *scene, ARegion *ar, View3D *v3d, int mode)
                rv3d->dist = m_dist;
                upvec[0] = upvec[1] = 0;
                upvec[2] = rv3d->dist;
-               Mat3CpyMat4(mat, rv3d->viewinv);
-               Mat3MulVecfl(mat, upvec);
-               VecAddf(rv3d->ofs, rv3d->ofs, upvec);
+               copy_m3_m4(mat, rv3d->viewinv);
+               mul_m3_v3(mat, upvec);
+               add_v3_v3v3(rv3d->ofs, rv3d->ofs, upvec);
        }
 
     /*----------------------------------------------------
@@ -2332,7 +2447,7 @@ void viewmoveNDOF(Scene *scene, ARegion *ar, View3D *v3d, int mode)
        fval[6] = fval[6] / 1000000.0f;
 
     // scale more if not in perspective mode
-       if (rv3d->persp == V3D_ORTHO) {
+       if (rv3d->persp == RV3D_ORTHO) {
                fval[0] = fval[0] * 0.05f;
                fval[1] = fval[1] * 0.05f;
                fval[2] = fval[2] * 0.05f;
@@ -2357,8 +2472,8 @@ void viewmoveNDOF(Scene *scene, ARegion *ar, View3D *v3d, int mode)
      d = 1.0f;
 
 /*    if (ob) {
-        VecSubf(diff, obofs, rv3d->ofs);
-        d = VecLength(diff);
+        sub_v3_v3v3(diff, obofs, rv3d->ofs);
+        d = len_v3(diff);
     }
 */
 
@@ -2373,15 +2488,15 @@ void viewmoveNDOF(Scene *scene, ARegion *ar, View3D *v3d, int mode)
     dvec[0] = curareaX * rv3d->persinv[0][0] + curareaY * rv3d->persinv[1][0];
     dvec[1] = curareaX * rv3d->persinv[0][1] + curareaY * rv3d->persinv[1][1];
     dvec[2] = curareaX * rv3d->persinv[0][2] + curareaY * rv3d->persinv[1][2];
-    VecAddf(rv3d->ofs, rv3d->ofs, dvec);
+    add_v3_v3v3(rv3d->ofs, rv3d->ofs, dvec);
 
     /*----------------------------------------------------
      * ndof device dolly
      */
     len = zsens * sbadjust * fval[2];
 
-    if (rv3d->persp==V3D_CAMOB) {
-        if(rv3d->persp==V3D_CAMOB) { /* This is stupid, please fix - TODO */
+    if (rv3d->persp==RV3D_CAMOB) {
+        if(rv3d->persp==RV3D_CAMOB) { /* This is stupid, please fix - TODO */
             rv3d->camzoom+= 10.0f * -len;
         }
         if (rv3d->camzoom < minZoom) rv3d->camzoom = minZoom;
@@ -2398,14 +2513,14 @@ void viewmoveNDOF(Scene *scene, ARegion *ar, View3D *v3d, int mode)
      */
 
     /* Get the 3x3 matrix and its inverse from the quaternion */
-    QuatToMat3(rv3d->viewquat, m);
-    Mat3Inv(m_inv,m);
+    quat_to_mat3( m,rv3d->viewquat);
+    invert_m3_m3(m_inv,m);
 
     /* Determine the direction of the x vector (for rotating up and down) */
     /* This can likely be compuated directly from the quaternion. */
-    Mat3MulVecfl(m_inv,xvec);
-    Mat3MulVecfl(m_inv,yvec);
-    Mat3MulVecfl(m_inv,zvec);
+    mul_m3_v3(m_inv,xvec);
+    mul_m3_v3(m_inv,yvec);
+    mul_m3_v3(m_inv,zvec);
 
     /* Perform the up/down rotation */
     phi = sbadjust * rsens * /*0.5f * */ fval[3]; /* spin vertically half as fast as horizontally */
@@ -2414,13 +2529,13 @@ void viewmoveNDOF(Scene *scene, ARegion *ar, View3D *v3d, int mode)
     q1[1] = si * xvec[0];
     q1[2] = si * xvec[1];
     q1[3] = si * xvec[2];
-    QuatMul(rv3d->viewquat, rv3d->viewquat, q1);
+    mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
 
     if (use_sel) {
-        QuatConj(q1); /* conj == inv for unit quat */
-        VecSubf(rv3d->ofs, rv3d->ofs, obofs);
-        QuatMulVecf(q1, rv3d->ofs);
-        VecAddf(rv3d->ofs, rv3d->ofs, obofs);
+        conjugate_qt(q1); /* conj == inv for unit quat */
+        sub_v3_v3v3(rv3d->ofs, rv3d->ofs, obofs);
+        mul_qt_v3(q1, rv3d->ofs);
+        add_v3_v3v3(rv3d->ofs, rv3d->ofs, obofs);
     }
 
     /* Perform the orbital rotation */
@@ -2439,13 +2554,13 @@ void viewmoveNDOF(Scene *scene, ARegion *ar, View3D *v3d, int mode)
     q1[0] = cos(phi);
     q1[1] = q1[2] = 0.0;
     q1[3] = sin(phi);
-    QuatMul(rv3d->viewquat, rv3d->viewquat, q1);
+    mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
 
     if (use_sel) {
-        QuatConj(q1);
-        VecSubf(rv3d->ofs, rv3d->ofs, obofs);
-        QuatMulVecf(q1, rv3d->ofs);
-        VecAddf(rv3d->ofs, rv3d->ofs, obofs);
+        conjugate_qt(q1);
+        sub_v3_v3v3(rv3d->ofs, rv3d->ofs, obofs);
+        mul_qt_v3(q1, rv3d->ofs);
+        add_v3_v3v3(rv3d->ofs, rv3d->ofs, obofs);
     }
 
     /*----------------------------------------------------