Sculpt:
[blender.git] / source / blender / editors / space_view3d / view3d_edit.c
index 581e427..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"
@@ -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++) {
@@ -275,7 +275,7 @@ 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);
@@ -299,7 +299,7 @@ static void viewops_data(bContext *C, wmOperator *op, wmEvent *event)
                /* If there's no selection, lastofs is unmodified and last value since static */
                calculateTransformCenter(C, event, V3D_CENTROID, lastofs);
                VECCOPY(vod->obofs, lastofs);
-               VecMulf(vod->obofs, -1.0f);
+               mul_v3_fl(vod->obofs, -1.0f);
        }
 
        /* lookup, we dont pass on v3d to prevent confusement */
@@ -314,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 **********************************/
@@ -422,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] */
@@ -447,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 {
@@ -475,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);
@@ -489,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 */
@@ -503,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);
                }
        }
 
@@ -519,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]);
 
@@ -578,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;
        }
@@ -597,7 +610,7 @@ 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: */
@@ -678,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);
@@ -718,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;
        }
@@ -730,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);
@@ -868,9 +880,9 @@ static void viewzoom_apply(ViewOpsData *vod, int x, int y)
                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;
@@ -911,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;
        }
@@ -974,7 +984,7 @@ static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
        }
        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);
@@ -1001,7 +1011,7 @@ void VIEW3D_OT_zoom(wmOperatorType *ot)
        /* flags */
        ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
 
-       RNA_def_int(ot->srna, "delta", 0, 0, INT_MAX, "Delta", "", 0, INT_MAX);
+       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);
 }
@@ -1142,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);
                                        }
                                }
@@ -1232,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)
@@ -1401,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];
@@ -1430,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 */
@@ -1498,27 +1553,55 @@ static EnumPropertyItem prop_view_items[] = {
        {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==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;
-       }
-
        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));
@@ -1548,35 +1631,36 @@ static int viewnumpad_exec(bContext *C, wmOperator *op)
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
        Scene *scene= CTX_data_scene(C);
        static int perspo=RV3D_PERSP;
-       int viewnum;
+       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 RV3D_VIEW_BOTTOM :
-                       axis_set_view(C, 0.0, -1.0, 0.0, 0.0, viewnum, perspo);
+                       axis_set_view(C, 0.0, -1.0, 0.0, 0.0, viewnum, perspo, align_active);
                        break;
 
                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);
+                       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 RV3D_VIEW_LEFT:
-                       axis_set_view(C, 0.5, -0.5, 0.5, 0.5, viewnum, perspo);
+                       axis_set_view(C, 0.5, -0.5, 0.5, 0.5, viewnum, perspo, align_active);
                        break;
 
                case RV3D_VIEW_TOP:
-                       axis_set_view(C, 1.0, 0.0, 0.0, 0.0, viewnum, perspo);
+                       axis_set_view(C, 1.0, 0.0, 0.0, 0.0, viewnum, perspo, align_active);
                        break;
 
                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);
+                       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 RV3D_VIEW_RIGHT:
-                       axis_set_view(C, 0.5, -0.5, -0.5, -0.5, viewnum, perspo);
+                       axis_set_view(C, 0.5, -0.5, -0.5, -0.5, viewnum, perspo, align_active);
                        break;
 
                case RV3D_VIEW_CAMERA:
@@ -1617,7 +1701,7 @@ static int viewnumpad_exec(bContext *C, wmOperator *op)
                                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;
@@ -1645,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[] = {
@@ -1674,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);
@@ -1689,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);
@@ -1799,6 +1884,42 @@ 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);
@@ -1894,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 {
 
@@ -1951,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);
 
@@ -2181,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;
        }
 
@@ -2197,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);
        }
 
 
@@ -2219,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);
 
 
        /*----------------------------------------------------
@@ -2284,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);
        }
 
     /*----------------------------------------------------
@@ -2348,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);
     }
 */
 
@@ -2364,7 +2488,7 @@ 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
@@ -2389,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 */
@@ -2405,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 */
@@ -2430,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);
     }
 
     /*----------------------------------------------------