Merged changes in the trunk up to revision 45383.
[blender.git] / source / blender / editors / transform / transform_orientations.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Martin Poirier
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/transform/transform_orientations.c
24  *  \ingroup edtransform
25  */
26
27
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_armature_types.h"
34 #include "DNA_curve_types.h"
35 #include "DNA_mesh_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_view3d_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_armature.h"
46 #include "BKE_curve.h"
47 #include "BKE_context.h"
48 #include "BKE_tessmesh.h"
49 #include "BKE_report.h"
50
51 #include "BLF_translation.h"
52
53 //#include "BIF_editmesh.h"
54 //#include "BIF_interface.h"
55 //#include "BIF_space.h"
56 //#include "BIF_toolbox.h"
57
58 #include "ED_armature.h"
59 #include "ED_mesh.h"
60
61 #include "RNA_define.h"
62
63 #include "UI_interface.h"
64
65 #include "transform.h"
66
67 /* *********************** TransSpace ************************** */
68
69 void BIF_clearTransformOrientation(bContext *C)
70 {
71         View3D *v3d = CTX_wm_view3d(C);
72
73         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
74         BLI_freelistN(transform_spaces);
75         
76         // Need to loop over all view3d
77         if (v3d && v3d->twmode >= V3D_MANIP_CUSTOM) {
78                 v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
79         }
80 }
81
82 static TransformOrientation* findOrientationName(ListBase *lb, const char *name)
83 {
84         TransformOrientation *ts= NULL;
85
86         for (ts= lb->first; ts; ts = ts->next) {
87                 if (strncmp(ts->name, name, sizeof(ts->name)-1) == 0) {
88                         return ts;
89                 }
90         }
91         
92         return NULL;
93 }
94
95 static int uniqueOrientationNameCheck(void *arg, const char *name)
96 {
97         return findOrientationName((ListBase *)arg, name) != NULL;
98 }
99
100 static void uniqueOrientationName(ListBase *lb, char *name)
101 {
102         BLI_uniquename_cb(uniqueOrientationNameCheck, lb, "Space", '.', name, sizeof(((TransformOrientation *)NULL)->name));
103 }
104
105 void BIF_createTransformOrientation(bContext *C, ReportList *reports, char *name, int use, int overwrite)
106 {
107         Object *obedit = CTX_data_edit_object(C);
108         Object *ob = CTX_data_active_object(C);
109         TransformOrientation *ts = NULL;
110         
111         if (obedit) {
112                 if (obedit->type == OB_MESH)
113                         ts = createMeshSpace(C, reports, name, overwrite);
114                 else if (obedit->type == OB_ARMATURE)
115                         ts = createBoneSpace(C, reports, name, overwrite);
116         }
117         else if (ob && (ob->mode & OB_MODE_POSE)) {
118                         ts = createBoneSpace(C, reports, name, overwrite);
119         }
120         else {
121                 ts = createObjectSpace(C, reports, name, overwrite);
122         }
123         
124         if (use && ts != NULL) {
125                 BIF_selectTransformOrientation(C, ts);
126         }
127 }
128
129 TransformOrientation *createObjectSpace(bContext *C, ReportList *UNUSED(reports), char *name, int overwrite)
130 {
131         Base *base = CTX_data_active_base(C);
132         Object *ob;
133         float mat[3][3];
134
135         if (base == NULL)
136                 return NULL;
137
138
139         ob = base->object;
140         
141         copy_m3_m4(mat, ob->obmat);
142         normalize_m3(mat);
143
144         /* use object name if no name is given */
145         if (name[0] == 0) {
146                 strncpy(name, ob->id.name+2, MAX_ID_NAME-2);
147         }
148
149         return addMatrixSpace(C, mat, name, overwrite); 
150 }
151
152 TransformOrientation *createBoneSpace(bContext *C, ReportList *reports, char *name, int overwrite)
153 {
154         float mat[3][3];
155         float normal[3], plane[3];
156
157         getTransformOrientation(C, normal, plane, 0);
158
159         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
160                 BKE_reports_prepend(reports, "Cannot use zero-length bone");
161                 return NULL;
162         }
163
164         if (name[0] == 0) {
165                 strcpy(name, "Bone");
166         }
167
168         return addMatrixSpace(C, mat, name, overwrite);
169 }
170
171 TransformOrientation *createMeshSpace(bContext *C, ReportList *reports, char *name, int overwrite)
172 {
173         float mat[3][3];
174         float normal[3], plane[3];
175         int type;
176
177         type = getTransformOrientation(C, normal, plane, 0);
178         
179         switch (type)
180         {
181                 case ORIENTATION_VERT:
182                         if (createSpaceNormal(mat, normal) == 0) {
183                                 BKE_reports_prepend(reports, "Cannot use vertex with zero-length normal");
184                                 return NULL;
185                         }
186         
187                         if (name[0] == 0) {
188                                 strcpy(name, "Vertex");
189                         }
190                         break;
191                 case ORIENTATION_EDGE:
192                         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
193                                 BKE_reports_prepend(reports, "Cannot use zero-length edge");
194                                 return NULL;
195                         }
196         
197                         if (name[0] == 0) {
198                                 strcpy(name, "Edge");
199                         }
200                         break;
201                 case ORIENTATION_FACE:
202                         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
203                                 BKE_reports_prepend(reports, "Cannot use zero-area face");
204                                 return NULL;
205                         }
206         
207                         if (name[0] == 0) {
208                                 strcpy(name, "Face");
209                         }
210                         break;
211                 default:
212                         return NULL;
213                         break;
214         }
215
216         return addMatrixSpace(C, mat, name, overwrite);
217 }
218
219 int createSpaceNormal(float mat[3][3], float normal[3])
220 {
221         float tangent[3] = {0.0f, 0.0f, 1.0f};
222         
223         copy_v3_v3(mat[2], normal);
224         if (normalize_v3(mat[2]) == 0.0f) {
225                 return 0; /* error return */
226         }
227
228         cross_v3_v3v3(mat[0], mat[2], tangent);
229         if (dot_v3v3(mat[0], mat[0]) == 0.0f) {
230                 tangent[0] = 1.0f;
231                 tangent[1] = tangent[2] = 0.0f;
232                 cross_v3_v3v3(mat[0], tangent, mat[2]);
233         }
234
235         cross_v3_v3v3(mat[1], mat[2], mat[0]);
236
237         normalize_m3(mat);
238         
239         return 1;
240 }
241
242 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3])
243 {
244         copy_v3_v3(mat[2], normal);
245         if (normalize_v3(mat[2]) == 0.0f) {
246                 return 0; /* error return */
247         }
248         
249         /* preempt zero length tangent from causing trouble */
250         if (tangent[0] == 0 && tangent[1] == 0 && tangent[2] == 0) {
251                 tangent[2] = 1;
252         }
253
254         cross_v3_v3v3(mat[0], mat[2], tangent);
255         if (normalize_v3(mat[0]) == 0.0f) {
256                 return 0; /* error return */
257         }
258         
259         cross_v3_v3v3(mat[1], mat[2], mat[0]);
260
261         normalize_m3(mat);
262         
263         return 1;
264 }
265
266 TransformOrientation* addMatrixSpace(bContext *C, float mat[3][3], char name[], int overwrite)
267 {
268         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
269         TransformOrientation *ts = NULL;
270
271         if (overwrite) {
272                 ts = findOrientationName(transform_spaces, name);
273         }
274         else {
275                 uniqueOrientationName(transform_spaces, name);
276         }
277
278         /* if not, create a new one */
279         if (ts == NULL) {
280                 ts = MEM_callocN(sizeof(TransformOrientation), "UserTransSpace from matrix");
281                 BLI_addtail(transform_spaces, ts);
282                 strncpy(ts->name, name, sizeof(ts->name));
283         }
284
285         /* copy matrix into transform space */
286         copy_m3_m3(ts->mat, mat);
287
288         return ts;
289 }
290
291 void BIF_removeTransformOrientation(bContext *C, TransformOrientation *target)
292 {
293         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
294         TransformOrientation *ts;
295         int i;
296         
297         for (i = 0, ts = transform_spaces->first; ts; ts = ts->next, i++) {
298                 if (ts == target) {
299                         View3D *v3d = CTX_wm_view3d(C);
300                         if (v3d) {
301                                 int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
302                                 
303                                 // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
304                                 if (selected_index == i) {
305                                         v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
306                                 }
307                                 else if (selected_index > i) {
308                                         v3d->twmode--;
309                                 }
310                                 
311                         }
312
313                         BLI_freelinkN(transform_spaces, ts);
314                         break;
315                 }
316         }
317 }
318
319 void BIF_removeTransformOrientationIndex(bContext *C, int index)
320 {
321         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
322         TransformOrientation *ts= BLI_findlink(transform_spaces, index);
323
324         if (ts) {
325                 View3D *v3d = CTX_wm_view3d(C);
326                 if (v3d) {
327                         int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
328                         
329                         // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
330                         if (selected_index == index) {
331                                 v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
332                         }
333                         else if (selected_index > index) {
334                                 v3d->twmode--;
335                         }
336                         
337                 }
338
339                 BLI_freelinkN(transform_spaces, ts);
340         }
341 }
342
343 void BIF_selectTransformOrientation(bContext *C, TransformOrientation *target)
344 {
345         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
346         View3D *v3d = CTX_wm_view3d(C);
347         TransformOrientation *ts;
348         int i;
349         
350         for (i = 0, ts = transform_spaces->first; ts; ts = ts->next, i++) {
351                 if (ts == target) {
352                         v3d->twmode = V3D_MANIP_CUSTOM + i;
353                         break;
354                 }
355         }
356 }
357
358 void BIF_selectTransformOrientationValue(bContext *C, int orientation)
359 {
360         View3D *v3d = CTX_wm_view3d(C);
361         if (v3d) /* currently using generic poll */
362                 v3d->twmode = orientation;
363 }
364
365 EnumPropertyItem *BIF_enumTransformOrientation(bContext *C)
366 {
367         Scene *scene;
368         ListBase *transform_spaces;
369         TransformOrientation *ts= NULL;
370
371         EnumPropertyItem global = {V3D_MANIP_GLOBAL, "GLOBAL", 0, "Global", ""};
372         EnumPropertyItem normal = {V3D_MANIP_NORMAL, "NORMAL", 0, "Normal", ""};
373         EnumPropertyItem local = {V3D_MANIP_LOCAL, "LOCAL", 0, "Local", ""};
374         EnumPropertyItem view = {V3D_MANIP_VIEW, "VIEW", 0, "View", ""};
375         EnumPropertyItem tmp = {0, "", 0, "", ""};
376         EnumPropertyItem *item= NULL;
377         int i = V3D_MANIP_CUSTOM, totitem= 0;
378
379         RNA_enum_item_add(&item, &totitem, &global);
380         RNA_enum_item_add(&item, &totitem, &normal);
381         RNA_enum_item_add(&item, &totitem, &local);
382         RNA_enum_item_add(&item, &totitem, &view);
383
384         if (C) {
385                 scene= CTX_data_scene(C);
386
387                 if (scene) {
388                         transform_spaces = &scene->transform_spaces;
389                         ts = transform_spaces->first;
390                 }
391         }
392                 
393         if (ts)
394                 RNA_enum_item_add_separator(&item, &totitem);
395
396         for (; ts; ts = ts->next) {
397                 tmp.identifier = "CUSTOM";
398                 tmp.name= ts->name;
399                 tmp.value = i++;
400                 RNA_enum_item_add(&item, &totitem, &tmp);
401         }
402
403         RNA_enum_item_end(&item, &totitem);
404
405         return item;
406 }
407
408 const char * BIF_menustringTransformOrientation(const bContext *C, const char *title)
409 {
410         const char* menu = IFACE_("%t|Global%x0|Local%x1|Gimbal%x4|Normal%x2|View%x3");
411         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
412         TransformOrientation *ts;
413         int i = V3D_MANIP_CUSTOM;
414         char *str_menu, *p;
415         const int elem_size = MAX_NAME + 4;
416
417         title = IFACE_(title);
418
419         str_menu = MEM_callocN(strlen(menu) + strlen(title) + 1 + elem_size * BIF_countTransformOrientation(C), TIP_("UserTransSpace from matrix"));
420         p = str_menu;
421         
422         p += sprintf(str_menu, "%s", title);
423         p += sprintf(p, "%s", menu);
424         
425         for (ts = transform_spaces->first; ts; ts = ts->next) {
426                 p += sprintf(p, "|%s%%x%d", ts->name, i++);
427         }
428         
429         return str_menu;
430 }
431
432 int BIF_countTransformOrientation(const bContext *C)
433 {
434         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
435         TransformOrientation *ts;
436         int count = 0;
437
438         for (ts = transform_spaces->first; ts; ts = ts->next) {
439                 count++;
440         }
441         
442         return count;
443 }
444
445 void applyTransformOrientation(const bContext *C, float mat[3][3], char *name)
446 {
447         TransformOrientation *ts;
448         View3D *v3d = CTX_wm_view3d(C);
449         int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
450         int i;
451         
452         if (selected_index >= 0) {
453                 for (i = 0, ts = CTX_data_scene(C)->transform_spaces.first; ts; ts = ts->next, i++) {
454                         if (selected_index == i) {
455                                 
456                                 if (name)
457                                         strcpy(name, ts->name);
458                                 
459                                 copy_m3_m3(mat, ts->mat);
460                                 break;
461                         }
462                 }
463           }
464 }
465
466 static int count_bone_select(bArmature *arm, ListBase *lb, int do_it) 
467 {
468         Bone *bone;
469         int do_next;
470         int total = 0;
471         
472         for (bone= lb->first; bone; bone= bone->next) {
473                 bone->flag &= ~BONE_TRANSFORM;
474                 do_next = do_it;
475                 if (do_it) {
476                         if (bone->layer & arm->layer) {
477                                 if (bone->flag & BONE_SELECTED) {
478                                         bone->flag |= BONE_TRANSFORM;
479                                         total++;
480                                         do_next= 0;     // no transform on children if one parent bone is selected
481                                 }
482                         }
483                 }
484                 total += count_bone_select(arm, &bone->childbase, do_next);
485         }
486         
487         return total;
488 }
489
490 void initTransformOrientation(bContext *C, TransInfo *t)
491 {
492         View3D *v3d = CTX_wm_view3d(C);
493         Object *ob = CTX_data_active_object(C);
494         Object *obedit = CTX_data_active_object(C);
495
496         switch(t->current_orientation) {
497         case V3D_MANIP_GLOBAL:
498                 strcpy(t->spacename, "global");
499                 break;
500
501         case V3D_MANIP_GIMBAL:
502                 unit_m3(t->spacemtx);
503                 if (gimbal_axis(ob, t->spacemtx)) {
504                         strcpy(t->spacename, "gimbal");
505                         break;
506                 }
507                 /* no gimbal fallthrough to normal */
508         case V3D_MANIP_NORMAL:
509                 if (obedit || (ob && ob->mode & OB_MODE_POSE)) {
510                         strcpy(t->spacename, "normal");
511                         ED_getTransformOrientationMatrix(C, t->spacemtx, (v3d->around == V3D_ACTIVE));
512                         break;
513                 }
514                 /* no break we define 'normal' as 'local' in Object mode */
515         case V3D_MANIP_LOCAL:
516                 strcpy(t->spacename, "local");
517                 
518                 if (ob) {
519                         copy_m3_m4(t->spacemtx, ob->obmat);
520                         normalize_m3(t->spacemtx);
521                 }
522                 else {
523                         unit_m3(t->spacemtx);
524                 }
525                 
526                 break;
527                 
528         case V3D_MANIP_VIEW:
529                 if (t->ar->regiontype == RGN_TYPE_WINDOW) {
530                         RegionView3D *rv3d = t->ar->regiondata;
531                         float mat[3][3];
532
533                         strcpy(t->spacename, "view");
534                         copy_m3_m4(mat, rv3d->viewinv);
535                         normalize_m3(mat);
536                         copy_m3_m3(t->spacemtx, mat);
537                 }
538                 else {
539                         unit_m3(t->spacemtx);
540                 }
541                 break;
542         default: /* V3D_MANIP_CUSTOM */
543                 applyTransformOrientation(C, t->spacemtx, t->spacename);
544                 break;
545         }
546 }
547
548 int getTransformOrientation(const bContext *C, float normal[3], float plane[3], int activeOnly)
549 {
550         Scene *scene = CTX_data_scene(C);
551         View3D *v3d = CTX_wm_view3d(C);
552         Object *obedit= CTX_data_edit_object(C);
553         Base *base;
554         Object *ob = OBACT;
555         int result = ORIENTATION_NONE;
556
557         normal[0] = normal[1] = normal[2] = 0;
558         plane[0] = plane[1] = plane[2] = 0;
559
560         if (obedit) {
561                 float imat[3][3], mat[3][3];
562                 
563                 /* we need the transpose of the inverse for a normal... */
564                 copy_m3_m4(imat, ob->obmat);
565                 
566                 invert_m3_m3(mat, imat);
567                 transpose_m3(mat);
568
569                 ob= obedit;
570
571                 if (ob->type==OB_MESH) {
572                         Mesh *me= ob->data;
573                         BMEditMesh *em = me->edit_btmesh;
574                         BMVert *eve;
575                         BMEditSelection ese;
576                         float vec[3]= {0,0,0};
577                         
578                         /* USE LAST SELECTED WITH ACTIVE */
579                         if (activeOnly && EDBM_editselection_active_get(em, &ese)) {
580                                 EDBM_editselection_normal(normal, &ese);
581                                 EDBM_editselection_plane(em, plane, &ese);
582                                 
583                                 switch (ese.htype)
584                                 {
585                                         case BM_VERT:
586                                                 result = ORIENTATION_VERT;
587                                                 break;
588                                         case BM_EDGE:
589                                                 result = ORIENTATION_EDGE;
590                                                 break;
591                                         case BM_FACE:
592                                                 result = ORIENTATION_FACE;
593                                                 break;
594                                 }
595                         }
596                         else {
597                                 if (em->bm->totfacesel >= 1) {
598                                         BMFace *efa;
599                                         BMIter iter;
600
601                                         BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
602                                                 if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
603                                                         add_v3_v3(normal, efa->no);
604                                                         sub_v3_v3v3(vec,
605                                                                     BM_FACE_FIRST_LOOP(efa)->v->co,
606                                                                     BM_FACE_FIRST_LOOP(efa)->next->v->co);
607                                                         add_v3_v3(plane, vec);
608                                                 }
609                                         }
610                                         
611                                         result = ORIENTATION_FACE;
612                                 }
613                                 else if (em->bm->totvertsel == 3) {
614                                         BMVert *v1 = NULL, *v2 = NULL, *v3 = NULL;
615                                         BMIter iter;
616                                         float cotangent[3];
617                                         
618                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
619                                                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
620                                                         if (v1 == NULL) {
621                                                                 v1 = eve; 
622                                                         }
623                                                         else if (v2 == NULL) {
624                                                                 v2 = eve;
625                                                         }
626                                                         else {
627                                                                 v3 = eve;
628
629                                                                 sub_v3_v3v3(plane, v2->co, v1->co);
630                                                                 sub_v3_v3v3(cotangent, v3->co, v2->co);
631                                                                 cross_v3_v3v3(normal, cotangent, plane);
632                                                                 break;
633                                                         }
634                                                 }
635                                         }
636
637                                         /* if there's an edge available, use that for the tangent */
638                                         if (em->bm->totedgesel >= 1) {
639                                                 BMEdge *eed = NULL;
640                                                 BMIter iter;
641                                                 
642                                                 BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
643                                                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
644                                                                 sub_v3_v3v3(plane, eed->v2->co, eed->v1->co);
645                                                                 break;
646                                                         }
647                                                 }
648                                         }
649
650                                         result = ORIENTATION_FACE;
651                                 }
652                                 else if (em->bm->totedgesel == 1) {
653                                         BMEdge *eed = NULL;
654                                         BMIter iter;
655                                         
656                                         BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
657                                                 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
658                                                         /* use average vert normals as plane and edge vector as normal */
659                                                         copy_v3_v3(plane, eed->v1->no);
660                                                         add_v3_v3(plane, eed->v2->no);
661                                                         sub_v3_v3v3(normal, eed->v2->co, eed->v1->co);
662                                                         break;
663                                                 }
664                                         }
665                                         result = ORIENTATION_EDGE;
666                                 }
667                                 else if (em->bm->totvertsel == 2) {
668                                         BMVert *v1 = NULL, *v2 = NULL;
669                                         BMIter iter;
670
671                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
672                                                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
673                                                         if (v1 == NULL) {
674                                                                 v1 = eve; 
675                                                         }
676                                                         else {
677                                                                 v2 = eve;
678                                                                 
679                                                                 copy_v3_v3(plane, v1->no);
680                                                                 add_v3_v3(plane, v2->no);
681                                                                 sub_v3_v3v3(normal, v2->co, v1->co);
682                                                                 break; 
683                                                         }
684                                                 }
685                                         }
686                                         result = ORIENTATION_EDGE;
687                                 }
688                                 else if (em->bm->totvertsel == 1) {
689                                         BMIter iter;
690
691                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
692                                                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
693                                                         copy_v3_v3(normal, eve->no);
694                                                         break;
695                                                 }
696                                         }
697                                         result = ORIENTATION_VERT;
698                                 }
699                                 else if (em->bm->totvertsel > 3) {
700                                         BMIter iter;
701
702                                         zero_v3(normal);
703
704                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
705                                                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
706                                                         add_v3_v3(normal, eve->no);
707                                                 }
708                                         }
709                                         normalize_v3(normal);
710                                         result = ORIENTATION_VERT;
711                                 }
712                         }
713                 } /* end editmesh */
714                 else if (ELEM(obedit->type, OB_CURVE, OB_SURF)) {
715                         Curve *cu= obedit->data;
716                         Nurb *nu;
717                         BezTriple *bezt;
718                         int a;
719                         ListBase *nurbs= curve_editnurbs(cu);
720
721                         for (nu = nurbs->first; nu; nu = nu->next) {
722                                 /* only bezier has a normal */
723                                 if (nu->type == CU_BEZIER) {
724                                         bezt= nu->bezt;
725                                         a= nu->pntsu;
726                                         while (a--)
727                                         {
728                                                 /* exception */
729                                                 if ((bezt->f1 & SELECT) + (bezt->f2 & SELECT) + (bezt->f3 & SELECT) > SELECT) {
730                                                         sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[2]);
731                                                 }
732                                                 else {
733                                                         if (bezt->f1) {
734                                                                 sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[1]);
735                                                         }
736                                                         if (bezt->f2) {
737                                                                 sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[2]);
738                                                         }
739                                                         if (bezt->f3) {
740                                                                 sub_v3_v3v3(normal, bezt->vec[1], bezt->vec[2]);
741                                                         }
742                                                 }
743                                                 bezt++;
744                                         }
745                                 }
746                         }
747                         
748                         if (normal[0] != 0 || normal[1] != 0 || normal[2] != 0) {
749                                 result = ORIENTATION_NORMAL;
750                         }
751                 }
752                 else if (obedit->type==OB_MBALL) {
753 #if 0 // XXX
754                         /* editmball.c */
755                         MetaElem *ml, *ml_sel = NULL;
756         
757                         /* loop and check that only one element is selected */  
758                         for (ml = editelems.first; ml; ml = ml->next) {
759                                 if (ml->flag & SELECT) {
760                                         if (ml_sel == NULL) {
761                                                 ml_sel = ml;
762                                         }
763                                         else {
764                                                 ml_sel = NULL;
765                                                 break;
766                                         }
767                                 }
768                         }
769                         
770                         if (ml_sel) {   
771                                 float mat[4][4];
772
773                                 /* Rotation of MetaElem is stored in quat */
774                                 quat_to_mat4( mat,ml_sel->quat);
775
776                                 copy_v3_v3(normal, mat[2]);
777
778                                 negate_v3_v3(plane, mat[1]);
779                                 
780                                 result = ORIENTATION_NORMAL;
781                         }
782 #endif
783                         
784                 }
785                 else if (obedit->type == OB_ARMATURE) {
786                         bArmature *arm = obedit->data;
787                         EditBone *ebone;
788                         
789                         for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
790                                 if (arm->layer & ebone->layer) {
791                                         if (ebone->flag & BONE_SELECTED) {
792                                                 float tmat[3][3];
793                                                 float vec[3];
794                                                 sub_v3_v3v3(vec, ebone->tail, ebone->head);
795                                                 normalize_v3(vec);
796                                                 add_v3_v3(normal, vec);
797                                                 
798                                                 vec_roll_to_mat3(vec, ebone->roll, tmat);
799                                                 add_v3_v3(plane, tmat[2]);
800                                         }
801                                 }
802                         }
803                         
804                         normalize_v3(normal);
805                         normalize_v3(plane);
806
807                         if (plane[0] != 0 || plane[1] != 0 || plane[2] != 0) {
808                                 result = ORIENTATION_EDGE;
809                         }
810
811                 }
812
813                 /* Vectors from edges don't need the special transpose inverse multiplication */
814                 if (result == ORIENTATION_EDGE) {
815                         mul_mat3_m4_v3(ob->obmat, normal);
816                         mul_mat3_m4_v3(ob->obmat, plane);
817                 }
818                 else {
819                         mul_m3_v3(mat, normal);
820                         mul_m3_v3(mat, plane);
821                 }
822         }
823         else if (ob && (ob->mode & OB_MODE_POSE)) {
824                 bArmature *arm= ob->data;
825                 bPoseChannel *pchan;
826                 int totsel;
827                 
828                 totsel = count_bone_select(arm, &arm->bonebase, 1);
829                 if (totsel) {
830                         float imat[3][3], mat[3][3];
831
832                         /* use channels to get stats */
833                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
834                                 if (pchan->bone && pchan->bone->flag & BONE_TRANSFORM) {
835                                         add_v3_v3(normal, pchan->pose_mat[2]);
836                                         add_v3_v3(plane, pchan->pose_mat[1]);
837                                 }
838                         }
839                         negate_v3(plane);
840                         
841                         /* we need the transpose of the inverse for a normal... */
842                         copy_m3_m4(imat, ob->obmat);
843                         
844                         invert_m3_m3(mat, imat);
845                         transpose_m3(mat);
846                         mul_m3_v3(mat, normal);
847                         mul_m3_v3(mat, plane);
848                         
849                         result = ORIENTATION_EDGE;
850                 }
851         }
852         else if (ob && (ob->mode & (OB_MODE_ALL_PAINT|OB_MODE_PARTICLE_EDIT))) {
853                 /* pass */
854         }
855         else {
856                 /* we need the one selected object, if its not active */
857                 ob = OBACT;
858                 if (ob && !(ob->flag & SELECT)) ob = NULL;
859                 
860                 for (base= scene->base.first; base; base= base->next) {
861                         if (TESTBASELIB(v3d, base)) {
862                                 if (ob == NULL) {
863                                         ob= base->object;
864                                         break;
865                                 }
866                         }
867                 }
868                 
869                 if (ob) {
870                         copy_v3_v3(normal, ob->obmat[2]);
871                         copy_v3_v3(plane, ob->obmat[1]);
872                 }
873                 result = ORIENTATION_NORMAL;
874         }
875         
876         return result;
877 }
878
879 void ED_getTransformOrientationMatrix(const bContext *C, float orientation_mat[][3], int activeOnly)
880 {
881         float normal[3]={0.0, 0.0, 0.0};
882         float plane[3]={0.0, 0.0, 0.0};
883
884         int type;
885
886         type = getTransformOrientation(C, normal, plane, activeOnly);
887
888         switch (type)
889         {
890                 case ORIENTATION_NORMAL:
891                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0) {
892                                 type = ORIENTATION_NONE;
893                         }
894                         break;
895                 case ORIENTATION_VERT:
896                         if (createSpaceNormal(orientation_mat, normal) == 0) {
897                                 type = ORIENTATION_NONE;
898                         }
899                         break;
900                 case ORIENTATION_EDGE:
901                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0) {
902                                 type = ORIENTATION_NONE;
903                         }
904                         break;
905                 case ORIENTATION_FACE:
906                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0) {
907                                 type = ORIENTATION_NONE;
908                         }
909                         break;
910         }
911
912         if (type == ORIENTATION_NONE) {
913                 unit_m3(orientation_mat);
914         }
915 }