merge with trunk at r31523
[blender.git] / source / blender / editors / transform / transform_orientations.c
index 210ab0d45a82b3bb500761a6fdfed098348c8979..2715f497cc12632af1de16c0d8277d0332f8470b 100644 (file)
@@ -37,6 +37,7 @@
 #include "BKE_utildefines.h"
 #include "BKE_armature.h"
 #include "BKE_context.h"
+#include "BKE_tessmesh.h"
 #include "BKE_report.h"
 
 #include "BLI_math.h"
@@ -69,9 +70,9 @@ void BIF_clearTransformOrientation(bContext *C)
        // Need to loop over all view3d
        if(v3d && v3d->twmode >= V3D_MANIP_CUSTOM) {
                v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
-       }
 }
-
+}
 TransformOrientation* findOrientationName(bContext *C, char *name)
 {
        ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
@@ -151,16 +152,16 @@ TransformOrientation *createObjectSpace(bContext *C, ReportList *reports, char *
 
 
        ob = base->object;
-       
+
        copy_m3_m4(mat, ob->obmat);
        normalize_m3(mat);
 
        /* use object name if no name is given */
        if (name[0] == 0)
-       {
+{
                strncpy(name, ob->id.name+2, 35);
        }
-
+       
        return addMatrixSpace(C, mat, name, overwrite); 
 }
 
@@ -169,7 +170,7 @@ TransformOrientation *createBoneSpace(bContext *C, ReportList *reports, char *na
        float normal[3], plane[3];
 
        getTransformOrientation(C, normal, plane, 0);
-
+       
        if (createSpaceNormalTangent(mat, normal, plane) == 0) {
                BKE_reports_prepend(reports, "Cannot use zero-length bone");
                return NULL;
@@ -177,7 +178,7 @@ TransformOrientation *createBoneSpace(bContext *C, ReportList *reports, char *na
 
        if (name[0] == 0)
        {
-               strcpy(name, "Bone");
+       strcpy(name, "Bone");
        }
 
        return addMatrixSpace(C, mat, name, overwrite);
@@ -200,7 +201,7 @@ TransformOrientation *createMeshSpace(bContext *C, ReportList *reports, char *na
        
                        if (name[0] == 0)
                        {
-                               strcpy(name, "Vertex");
+                       strcpy(name, "Vertex");
                        }
                        break;
                case ORIENTATION_EDGE:
@@ -211,7 +212,7 @@ TransformOrientation *createMeshSpace(bContext *C, ReportList *reports, char *na
        
                        if (name[0] == 0)
                        {
-                               strcpy(name, "Edge");
+                       strcpy(name, "Edge");
                        }
                        break;
                case ORIENTATION_FACE:
@@ -222,7 +223,7 @@ TransformOrientation *createMeshSpace(bContext *C, ReportList *reports, char *na
        
                        if (name[0] == 0)
                        {
-                               strcpy(name, "Face");
+                       strcpy(name, "Face");
                        }
                        break;
                default:
@@ -288,7 +289,7 @@ TransformOrientation* addMatrixSpace(bContext *C, float mat[3][3], char name[],
        if (overwrite)
        {
                ts = findOrientationName(C, name);
-       }
+               }
        else
        {
                uniqueOrientationName(C, name);
@@ -319,14 +320,14 @@ void BIF_removeTransformOrientation(bContext *C, TransformOrientation *target) {
                        if(v3d) {
                                int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
                                
-                               // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
+                       // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
                                if (selected_index == i) {
                                        v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
                                }
                                else if (selected_index > i) {
                                        v3d->twmode--;
                                }
-                               
+
                        }
 
                        BLI_freelinkN(transform_spaces, ts);
@@ -379,7 +380,7 @@ void BIF_selectTransformOrientation(bContext *C, TransformOrientation *target) {
 void BIF_selectTransformOrientationValue(bContext *C, int orientation) {
        View3D *v3d = CTX_wm_view3d(C);
        if(v3d) /* currently using generic poll */
-               v3d->twmode = orientation;
+       v3d->twmode = orientation;
 }
 
 EnumPropertyItem *BIF_enumTransformOrientation(bContext *C)
@@ -525,15 +526,15 @@ void initTransformOrientation(bContext *C, TransInfo *t)
                if(obedit || (ob && ob->mode & OB_MODE_POSE)) {
                        strcpy(t->spacename, "normal");
                        ED_getTransformOrientationMatrix(C, t->spacemtx, (v3d->around == V3D_ACTIVE));
-                       break;
-               }
+                                       break;
+                                       }
                /* no break we define 'normal' as 'local' in Object mode */
        case V3D_MANIP_LOCAL:
                strcpy(t->spacename, "local");
                
                if(ob) {
-                       copy_m3_m4(t->spacemtx, ob->obmat);
-                       normalize_m3(t->spacemtx);
+               copy_m3_m4(t->spacemtx, ob->obmat);
+               normalize_m3(t->spacemtx);
                } else {
                        unit_m3(t->spacemtx);
                }
@@ -571,8 +572,8 @@ int getTransformOrientation(const bContext *C, float normal[3], float plane[3],
        Object *ob = OBACT;
        int result = ORIENTATION_NONE;
 
-       normal[0] = normal[1] = normal[2] = 0;
-       plane[0] = plane[1] = plane[2] = 0;
+       normal[0] = normal[1] = normal[2] = 0.0f;
+       plane[0] = plane[1] = plane[2] = 0.0f;
 
        if(obedit)
        {
@@ -589,56 +590,56 @@ int getTransformOrientation(const bContext *C, float normal[3], float plane[3],
                if(ob->type==OB_MESH)
                {
                        Mesh *me= ob->data;
-                       EditMesh *em = me->edit_mesh;
-                       EditVert *eve;
-                       EditSelection ese;
+                       BMEditMesh *em = me->edit_btmesh;
+                       BMVert *eve;
+                       BMEditSelection ese;
                        float vec[3]= {0,0,0};
                        
                        /* USE LAST SELECTED WITH ACTIVE */
-                       if (activeOnly && EM_get_actSelection(em, &ese))
+                       if (activeOnly && EDBM_get_actSelection(em, &ese))
                        {
-                               EM_editselection_normal(normal, &ese);
-                               EM_editselection_plane(plane, &ese);
+                               EDBM_editselection_normal(normal, &ese);
+                               EDBM_editselection_plane(em, plane, &ese);
                                
                                switch (ese.type)
                                {
-                                       case EDITVERT:
+                                       case BM_VERT:
                                                result = ORIENTATION_VERT;
                                                break;
-                                       case EDITEDGE:
+                                       case BM_EDGE:
                                                result = ORIENTATION_EDGE;
                                                break;
-                                       case EDITFACE:
+                                       case BM_FACE:
                                                result = ORIENTATION_FACE;
                                                break;
                                }
                        }
                        else
                        {
-                               if (em->totfacesel >= 1)
+                               if (em->bm->totfacesel >= 1)
                                {
-                                       EditFace *efa;
-                                       
-                                       for(efa= em->faces.first; efa; efa= efa->next)
-                                       {
-                                               if(efa->f & SELECT)
-                                               {
-                                                       VECADD(normal, normal, efa->n);
-                                                       sub_v3_v3v3(vec, efa->v2->co, efa->v1->co);
+                                       BMFace *efa;
+                                       BMIter iter;
+
+                                       BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
+                                               if(BM_TestHFlag(efa, BM_SELECT)) {
+                                                       VECADD(normal, normal, efa->no);
+                                                       sub_v3_v3v3(vec, ((BMLoopList*)efa->loops.first)->first->v->co, 
+                                                               (((BMLoopList*)efa->loops.first)->first->next)->v->co);
                                                        VECADD(plane, plane, vec);
                                                }
                                        }
                                        
                                        result = ORIENTATION_FACE;
                                }
-                               else if (em->totvertsel == 3)
+                               else if (em->bm->totvertsel == 3)
                                {
-                                       EditVert *v1 = NULL, *v2 = NULL, *v3 = NULL;
+                                       BMVert *v1 = NULL, *v2 = NULL, *v3 = NULL;
+                                       BMIter iter;
                                        float cotangent[3];
                                        
-                                       for (eve = em->verts.first; eve; eve = eve->next)
-                                       {
-                                               if ( eve->f & SELECT ) {
+                                       BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                                               if (BM_TestHFlag(eve, BM_SELECT)) {
                                                        if (v1 == NULL) {
                                                                v1 = eve; 
                                                        }
@@ -657,12 +658,13 @@ int getTransformOrientation(const bContext *C, float normal[3], float plane[3],
                                        }
 
                                        /* if there's an edge available, use that for the tangent */
-                                       if (em->totedgesel >= 1)
+                                       if (em->bm->totedgesel >= 1)
                                        {
-                                               EditEdge *eed = NULL;
-       
-                                               for(eed= em->edges.first; eed; eed= eed->next) {
-                                                       if(eed->f & SELECT) {
+                                               BMEdge *eed = NULL;
+                                               BMIter iter;
+                                               
+                                               BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
+                                                       if(BM_TestHFlag(eed, BM_SELECT)) {
                                                                sub_v3_v3v3(plane, eed->v2->co, eed->v1->co);
                                                                break;
                                                        }
@@ -671,12 +673,13 @@ int getTransformOrientation(const bContext *C, float normal[3], float plane[3],
 
                                        result = ORIENTATION_FACE;
                                }
-                               else if (em->totedgesel == 1)
+                               else if (em->bm->totedgesel == 1)
                                {
-                                       EditEdge *eed;
-
-                                       for(eed= em->edges.first; eed; eed= eed->next) {
-                                               if(eed->f & SELECT) {
+                                       BMEdge *eed = NULL;
+                                       BMIter iter;
+                                       
+                                       BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
+                                               if(BM_TestHFlag(eed, BM_SELECT)) {
                                                        /* use average vert normals as plane and edge vector as normal */
                                                        VECCOPY(plane, eed->v1->no);
                                                        VECADD(plane, plane, eed->v2->no);
@@ -686,13 +689,13 @@ int getTransformOrientation(const bContext *C, float normal[3], float plane[3],
                                        }
                                        result = ORIENTATION_EDGE;
                                }
-                               else if (em->totvertsel == 2)
+                               else if (em->bm->totvertsel == 2)
                                {
-                                       EditVert *v1 = NULL, *v2 = NULL;
-               
-                                       for (eve = em->verts.first; eve; eve = eve->next)
-                                       {
-                                               if ( eve->f & SELECT ) {
+                                       BMVert *v1 = NULL, *v2 = NULL;
+                                       BMIter iter;
+
+                                       BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                                               if (BM_TestHFlag(eve, BM_SELECT)) {
                                                        if (v1 == NULL) {
                                                                v1 = eve; 
                                                        }
@@ -708,24 +711,25 @@ int getTransformOrientation(const bContext *C, float normal[3], float plane[3],
                                        }
                                        result = ORIENTATION_EDGE;
                                }
-                               else if (em->totvertsel == 1)
+                               else if (em->bm->totvertsel == 1)
                                {
-                                       for (eve = em->verts.first; eve; eve = eve->next)
-                                       {
-                                               if ( eve->f & SELECT ) {
+                                       BMIter iter;
+
+                                       BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                                               if (BM_TestHFlag(eve, BM_SELECT)) {
                                                        VECCOPY(normal, eve->no);
                                                        break;
                                                }
                                        }
                                        result = ORIENTATION_VERT;
                                }
-                               else if (em->totvertsel > 3)
+                               else if (em->bm->totvertsel > 3)
                                {
-                                       normal[0] = normal[1] = normal[2] = 0;
-                                       
-                                       for (eve = em->verts.first; eve; eve = eve->next)
-                                       {
-                                               if ( eve->f & SELECT ) {
+                                       BMIter iter;
+                                       normal[0] = normal[1] = normal[2] = 0.0f;
+
+                                       BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                                               if (BM_TestHFlag(eve, BM_SELECT)) {
                                                        add_v3_v3(normal, eve->no);
                                                }
                                        }
@@ -911,10 +915,18 @@ int getTransformOrientation(const bContext *C, float normal[3], float plane[3],
                        }
                }
                
-               if (ob) {
+               if (!ob) {
+                       normal[0] = 0.0f;
+                       normal[1] = 0.0f;
+                       normal[2] = 1.0f;
+                       plane[0] = 1.0f;
+                       plane[1] = 0.0f;
+                       plane[2] = 0.0f;
+               } else {
                        VECCOPY(normal, ob->obmat[2]);
                        VECCOPY(plane, ob->obmat[1]);
                }
+
                result = ORIENTATION_NORMAL;
        }