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