svn merge ^/trunk/blender -r42991:43009
[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_object_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_view3d_types.h"
39
40
41 #include "BKE_armature.h"
42 #include "BKE_curve.h"
43 #include "BKE_context.h"
44 #include "BKE_tessmesh.h"
45 #include "BKE_report.h"
46
47 #include "BLI_math.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_editVert.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, 35);
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         {
281                 ts = findOrientationName(transform_spaces, name);
282         }
283         else
284         {
285                 uniqueOrientationName(transform_spaces, name);
286         }
287
288         /* if not, create a new one */
289         if (ts == NULL)
290         {
291                 ts = MEM_callocN(sizeof(TransformOrientation), "UserTransSpace from matrix");
292                 BLI_addtail(transform_spaces, ts);
293                 strncpy(ts->name, name, 35);
294         }
295
296         /* copy matrix into transform space */
297         copy_m3_m3(ts->mat, mat);
298
299         return ts;
300 }
301
302 void BIF_removeTransformOrientation(bContext *C, TransformOrientation *target)
303 {
304         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
305         TransformOrientation *ts;
306         int i;
307         
308         for (i = 0, ts = transform_spaces->first; ts; ts = ts->next, i++) {
309                 if (ts == target) {
310                         View3D *v3d = CTX_wm_view3d(C);
311                         if(v3d) {
312                                 int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
313                                 
314                                 // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
315                                 if (selected_index == i) {
316                                         v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
317                                 }
318                                 else if (selected_index > i) {
319                                         v3d->twmode--;
320                                 }
321                                 
322                         }
323
324                         BLI_freelinkN(transform_spaces, ts);
325                         break;
326                 }
327         }
328 }
329
330 void BIF_removeTransformOrientationIndex(bContext *C, int index)
331 {
332         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
333         TransformOrientation *ts= BLI_findlink(transform_spaces, index);
334
335         if (ts) {
336                 View3D *v3d = CTX_wm_view3d(C);
337                 if(v3d) {
338                         int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
339                         
340                         // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
341                         if (selected_index == index) {
342                                 v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
343                         }
344                         else if (selected_index > index) {
345                                 v3d->twmode--;
346                         }
347                         
348                 }
349
350                 BLI_freelinkN(transform_spaces, ts);
351         }
352 }
353
354 void BIF_selectTransformOrientation(bContext *C, TransformOrientation *target)
355 {
356         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
357         View3D *v3d = CTX_wm_view3d(C);
358         TransformOrientation *ts;
359         int i;
360         
361         for (i = 0, ts = transform_spaces->first; ts; ts = ts->next, i++) {
362                 if (ts == target) {
363                         v3d->twmode = V3D_MANIP_CUSTOM + i;
364                         break;
365                 }
366         }
367 }
368
369 void BIF_selectTransformOrientationValue(bContext *C, int orientation)
370 {
371         View3D *v3d = CTX_wm_view3d(C);
372         if(v3d) /* currently using generic poll */
373                 v3d->twmode = orientation;
374 }
375
376 EnumPropertyItem *BIF_enumTransformOrientation(bContext *C)
377 {
378         Scene *scene;
379         ListBase *transform_spaces;
380         TransformOrientation *ts= NULL;
381
382         EnumPropertyItem global = {V3D_MANIP_GLOBAL, "GLOBAL", 0, "Global", ""};
383         EnumPropertyItem normal = {V3D_MANIP_NORMAL, "NORMAL", 0, "Normal", ""};
384         EnumPropertyItem local = {V3D_MANIP_LOCAL, "LOCAL", 0, "Local", ""};
385         EnumPropertyItem view = {V3D_MANIP_VIEW, "VIEW", 0, "View", ""};
386         EnumPropertyItem tmp = {0, "", 0, "", ""};
387         EnumPropertyItem *item= NULL;
388         int i = V3D_MANIP_CUSTOM, totitem= 0;
389
390         RNA_enum_item_add(&item, &totitem, &global);
391         RNA_enum_item_add(&item, &totitem, &normal);
392         RNA_enum_item_add(&item, &totitem, &local);
393         RNA_enum_item_add(&item, &totitem, &view);
394
395         if(C) {
396                 scene= CTX_data_scene(C);
397
398                 if(scene) {
399                         transform_spaces = &scene->transform_spaces;
400                         ts = transform_spaces->first;
401                 }
402         }
403                 
404         if(ts)
405                 RNA_enum_item_add_separator(&item, &totitem);
406
407         for(; ts; ts = ts->next) {
408                 tmp.identifier = "CUSTOM";
409                 tmp.name= ts->name;
410                 tmp.value = i++;
411                 RNA_enum_item_add(&item, &totitem, &tmp);
412         }
413
414         RNA_enum_item_end(&item, &totitem);
415
416         return item;
417 }
418
419 const char * BIF_menustringTransformOrientation(const bContext *C, const char *title)
420 {
421         const char* menu = IFACE_("%t|Global%x0|Local%x1|Gimbal%x4|Normal%x2|View%x3");
422         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
423         TransformOrientation *ts;
424         int i = V3D_MANIP_CUSTOM;
425         char *str_menu, *p;
426
427         title = IFACE_(title);
428
429         str_menu = MEM_callocN(strlen(menu) + strlen(title) + 1 + 40 * BIF_countTransformOrientation(C), TIP_("UserTransSpace from matrix"));
430         p = str_menu;
431         
432         p += sprintf(str_menu, "%s", title);
433         p += sprintf(p, "%s", menu);
434         
435         for (ts = transform_spaces->first; ts; ts = ts->next) {
436                 p += sprintf(p, "|%s%%x%d", ts->name, i++);
437         }
438         
439         return str_menu;
440 }
441
442 int BIF_countTransformOrientation(const bContext *C)
443 {
444         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
445         TransformOrientation *ts;
446         int count = 0;
447
448         for (ts = transform_spaces->first; ts; ts = ts->next) {
449                 count++;
450         }
451         
452         return count;
453 }
454
455 void applyTransformOrientation(const bContext *C, float mat[3][3], char *name)
456 {
457         TransformOrientation *ts;
458         View3D *v3d = CTX_wm_view3d(C);
459         int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
460         int i;
461         
462         if (selected_index >= 0) {
463                 for (i = 0, ts = CTX_data_scene(C)->transform_spaces.first; ts; ts = ts->next, i++) {
464                         if (selected_index == i) {
465                                 
466                                 if (name)
467                                         strcpy(name, ts->name);
468                                 
469                                 copy_m3_m3(mat, ts->mat);
470                                 break;
471                         }
472                 }
473           }
474 }
475
476 static int count_bone_select(bArmature *arm, ListBase *lb, int do_it) 
477 {
478         Bone *bone;
479         int do_next;
480         int total = 0;
481         
482         for(bone= lb->first; bone; bone= bone->next) {
483                 bone->flag &= ~BONE_TRANSFORM;
484                 do_next = do_it;
485                 if(do_it) {
486                         if(bone->layer & arm->layer) {
487                                 if (bone->flag & BONE_SELECTED) {
488                                         bone->flag |= BONE_TRANSFORM;
489                                         total++;
490                                         do_next= 0;     // no transform on children if one parent bone is selected
491                                 }
492                         }
493                 }
494                 total += count_bone_select(arm, &bone->childbase, do_next);
495         }
496         
497         return total;
498 }
499
500 void initTransformOrientation(bContext *C, TransInfo *t)
501 {
502         View3D *v3d = CTX_wm_view3d(C);
503         Object *ob = CTX_data_active_object(C);
504         Object *obedit = CTX_data_active_object(C);
505
506         switch(t->current_orientation) {
507         case V3D_MANIP_GLOBAL:
508                 unit_m3(t->spacemtx);
509                 strcpy(t->spacename, "global");
510                 break;
511
512         case V3D_MANIP_GIMBAL:
513                 unit_m3(t->spacemtx);
514                 if (gimbal_axis(ob, t->spacemtx)) {
515                         strcpy(t->spacename, "gimbal");
516                         break;
517                 }
518                 /* no gimbal fallthrough to normal */
519         case V3D_MANIP_NORMAL:
520                 if(obedit || (ob && ob->mode & OB_MODE_POSE)) {
521                         strcpy(t->spacename, "normal");
522                         ED_getTransformOrientationMatrix(C, t->spacemtx, (v3d->around == V3D_ACTIVE));
523                         break;
524                 }
525                 /* no break we define 'normal' as 'local' in Object mode */
526         case V3D_MANIP_LOCAL:
527                 strcpy(t->spacename, "local");
528                 
529                 if(ob) {
530                         copy_m3_m4(t->spacemtx, ob->obmat);
531                         normalize_m3(t->spacemtx);
532                 } else {
533                         unit_m3(t->spacemtx);
534                 }
535                 
536                 break;
537                 
538         case V3D_MANIP_VIEW:
539                 if (t->ar->regiontype == RGN_TYPE_WINDOW)
540                 {
541                         RegionView3D *rv3d = t->ar->regiondata;
542                         float mat[3][3];
543
544                         strcpy(t->spacename, "view");
545                         copy_m3_m4(mat, rv3d->viewinv);
546                         normalize_m3(mat);
547                         copy_m3_m3(t->spacemtx, mat);
548                 }
549                 else
550                 {
551                         unit_m3(t->spacemtx);
552                 }
553                 break;
554         default: /* V3D_MANIP_CUSTOM */
555                 applyTransformOrientation(C, t->spacemtx, t->spacename);
556                 break;
557         }
558 }
559
560 int getTransformOrientation(const bContext *C, float normal[3], float plane[3], int activeOnly)
561 {
562         Scene *scene = CTX_data_scene(C);
563         View3D *v3d = CTX_wm_view3d(C);
564         Object *obedit= CTX_data_edit_object(C);
565         Base *base;
566         Object *ob = OBACT;
567         int result = ORIENTATION_NONE;
568
569         normal[0] = normal[1] = normal[2] = 0;
570         plane[0] = plane[1] = plane[2] = 0;
571
572         if(obedit)
573         {
574                 float imat[3][3], mat[3][3];
575                 
576                 /* we need the transpose of the inverse for a normal... */
577                 copy_m3_m4(imat, ob->obmat);
578                 
579                 invert_m3_m3(mat, imat);
580                 transpose_m3(mat);
581
582                 ob= obedit;
583
584                 if(ob->type==OB_MESH)
585                 {
586                         Mesh *me= ob->data;
587                         BMEditMesh *em = me->edit_btmesh;
588                         BMVert *eve;
589                         BMEditSelection ese;
590                         float vec[3]= {0,0,0};
591                         
592                         /* USE LAST SELECTED WITH ACTIVE */
593                         if (activeOnly && EDBM_get_actSelection(em, &ese))
594                         {
595                                 EDBM_editselection_normal(normal, &ese);
596                                 EDBM_editselection_plane(em, plane, &ese);
597                                 
598                                 switch (ese.htype)
599                                 {
600                                         case BM_VERT:
601                                                 result = ORIENTATION_VERT;
602                                                 break;
603                                         case BM_EDGE:
604                                                 result = ORIENTATION_EDGE;
605                                                 break;
606                                         case BM_FACE:
607                                                 result = ORIENTATION_FACE;
608                                                 break;
609                                 }
610                         }
611                         else
612                         {
613                                 if (em->bm->totfacesel >= 1)
614                                 {
615                                         BMFace *efa;
616                                         BMIter iter;
617
618                                         BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
619                                                 if(BM_TestHFlag(efa, BM_SELECT)) {
620                                                         add_v3_v3(normal, efa->no);
621                                                         sub_v3_v3v3(vec,
622                                                                     ((BMLoopList*)efa->loops.first)->first->v->co,
623                                                                     (((BMLoopList*)efa->loops.first)->first->next)->v->co);
624                                                         add_v3_v3(plane, vec);
625                                                 }
626                                         }
627                                         
628                                         result = ORIENTATION_FACE;
629                                 }
630                                 else if (em->bm->totvertsel == 3)
631                                 {
632                                         BMVert *v1 = NULL, *v2 = NULL, *v3 = NULL;
633                                         BMIter iter;
634                                         float cotangent[3];
635                                         
636                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
637                                                 if (BM_TestHFlag(eve, BM_SELECT)) {
638                                                         if (v1 == NULL) {
639                                                                 v1 = eve; 
640                                                         }
641                                                         else if (v2 == NULL) {
642                                                                 v2 = eve;
643                                                         }
644                                                         else {
645                                                                 v3 = eve;
646
647                                                                 sub_v3_v3v3(plane, v2->co, v1->co);
648                                                                 sub_v3_v3v3(cotangent, v3->co, v2->co);
649                                                                 cross_v3_v3v3(normal, cotangent, plane);
650                                                                 break;
651                                                         }
652                                                 }
653                                         }
654
655                                         /* if there's an edge available, use that for the tangent */
656                                         if (em->bm->totedgesel >= 1)
657                                         {
658                                                 BMEdge *eed = NULL;
659                                                 BMIter iter;
660                                                 
661                                                 BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
662                                                         if(BM_TestHFlag(eed, BM_SELECT)) {
663                                                                 sub_v3_v3v3(plane, eed->v2->co, eed->v1->co);
664                                                                 break;
665                                                         }
666                                                 }
667                                         }
668
669                                         result = ORIENTATION_FACE;
670                                 }
671                                 else if (em->bm->totedgesel == 1)
672                                 {
673                                         BMEdge *eed = NULL;
674                                         BMIter iter;
675                                         
676                                         BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
677                                                 if(BM_TestHFlag(eed, BM_SELECT)) {
678                                                         /* use average vert normals as plane and edge vector as normal */
679                                                         copy_v3_v3(plane, eed->v1->no);
680                                                         add_v3_v3(plane, eed->v2->no);
681                                                         sub_v3_v3v3(normal, eed->v2->co, eed->v1->co);
682                                                         break;
683                                                 }
684                                         }
685                                         result = ORIENTATION_EDGE;
686                                 }
687                                 else if (em->bm->totvertsel == 2)
688                                 {
689                                         BMVert *v1 = NULL, *v2 = NULL;
690                                         BMIter iter;
691
692                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
693                                                 if (BM_TestHFlag(eve, BM_SELECT)) {
694                                                         if (v1 == NULL) {
695                                                                 v1 = eve; 
696                                                         }
697                                                         else {
698                                                                 v2 = eve;
699                                                                 
700                                                                 copy_v3_v3(plane, v1->no);
701                                                                 add_v3_v3(plane, v2->no);
702                                                                 sub_v3_v3v3(normal, v2->co, v1->co);
703                                                                 break; 
704                                                         }
705                                                 }
706                                         }
707                                         result = ORIENTATION_EDGE;
708                                 }
709                                 else if (em->bm->totvertsel == 1)
710                                 {
711                                         BMIter iter;
712
713                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
714                                                 if (BM_TestHFlag(eve, BM_SELECT)) {
715                                                         copy_v3_v3(normal, eve->no);
716                                                         break;
717                                                 }
718                                         }
719                                         result = ORIENTATION_VERT;
720                                 }
721                                 else if (em->bm->totvertsel > 3)
722                                 {
723                                         BMIter iter;
724                                         normal[0] = normal[1] = normal[2] = 0.0f;
725
726                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
727                                                 if (BM_TestHFlag(eve, BM_SELECT)) {
728                                                         add_v3_v3(normal, eve->no);
729                                                 }
730                                         }
731                                         normalize_v3(normal);
732                                         result = ORIENTATION_VERT;
733                                 }
734                         }
735                 } /* end editmesh */
736                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
737                 {
738                         Curve *cu= obedit->data;
739                         Nurb *nu;
740                         BezTriple *bezt;
741                         int a;
742                         ListBase *nurbs= curve_editnurbs(cu);
743
744                         for (nu = nurbs->first; nu; nu = nu->next)
745                         {
746                                 /* only bezier has a normal */
747                                 if(nu->type == CU_BEZIER)
748                                 {
749                                         bezt= nu->bezt;
750                                         a= nu->pntsu;
751                                         while(a--)
752                                         {
753                                                 /* exception */
754                                                 if ( (bezt->f1 & SELECT) + (bezt->f2 & SELECT) + (bezt->f3 & SELECT) > SELECT )
755                                                 {
756                                                         sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[2]);
757                                                 }
758                                                 else
759                                                 {
760                                                         if(bezt->f1)
761                                                         {
762                                                                 sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[1]);
763                                                         }
764                                                         if(bezt->f2)
765                                                         {
766                                                                 sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[2]);
767                                                         }
768                                                         if(bezt->f3)
769                                                         {
770                                                                 sub_v3_v3v3(normal, bezt->vec[1], bezt->vec[2]);
771                                                         }
772                                                 }
773                                                 bezt++;
774                                         }
775                                 }
776                         }
777                         
778                         if (normal[0] != 0 || normal[1] != 0 || normal[2] != 0)
779                         {
780                                 result = ORIENTATION_NORMAL;
781                         }
782                 }
783                 else if(obedit->type==OB_MBALL)
784                 {
785 #if 0 // XXX
786                         /* editmball.c */
787                         extern ListBase editelems;  /* go away ! */
788                         MetaElem *ml, *ml_sel = NULL;
789         
790                         /* loop and check that only one element is selected */  
791                         for (ml = editelems.first; ml; ml = ml->next)
792                         {
793                                 if (ml->flag & SELECT) {
794                                         if (ml_sel == NULL)
795                                         {
796                                                 ml_sel = ml;
797                                         }
798                                         else
799                                         {
800                                                 ml_sel = NULL;
801                                                 break;
802                                         }
803                                 }
804                         }
805                         
806                         if (ml_sel)
807                         {       
808                                 float mat[4][4];
809
810                                 /* Rotation of MetaElem is stored in quat */
811                                 quat_to_mat4( mat,ml_sel->quat);
812
813                                 copy_v3_v3(normal, mat[2]);
814
815                                 negate_v3_v3(plane, mat[1]);
816                                 
817                                 result = ORIENTATION_NORMAL;
818                         }
819 #endif
820                         
821                 }
822                 else if (obedit->type == OB_ARMATURE)
823                 {
824                         bArmature *arm = obedit->data;
825                         EditBone *ebone;
826                         
827                         for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
828                                 if (arm->layer & ebone->layer)
829                                 {
830                                         if (ebone->flag & BONE_SELECTED)
831                                         {
832                                                 float tmat[3][3];
833                                                 float vec[3];
834                                                 sub_v3_v3v3(vec, ebone->tail, ebone->head);
835                                                 normalize_v3(vec);
836                                                 add_v3_v3(normal, vec);
837                                                 
838                                                 vec_roll_to_mat3(vec, ebone->roll, tmat);
839                                                 add_v3_v3(plane, tmat[2]);
840                                         }
841                                 }
842                         }
843                         
844                         normalize_v3(normal);
845                         normalize_v3(plane);
846
847                         if (plane[0] != 0 || plane[1] != 0 || plane[2] != 0)
848                         {
849                                 result = ORIENTATION_EDGE;
850                         }
851
852                 }
853
854                 /* Vectors from edges don't need the special transpose inverse multiplication */
855                 if (result == ORIENTATION_EDGE)
856                 {
857                         mul_mat3_m4_v3(ob->obmat, normal);
858                         mul_mat3_m4_v3(ob->obmat, plane);
859                 }
860                 else
861                 {
862                         mul_m3_v3(mat, normal);
863                         mul_m3_v3(mat, plane);
864                 }
865         }
866         else if(ob && (ob->mode & OB_MODE_POSE))
867         {
868                 bArmature *arm= ob->data;
869                 bPoseChannel *pchan;
870                 int totsel;
871                 
872                 totsel = count_bone_select(arm, &arm->bonebase, 1);
873                 if(totsel) {
874                         float imat[3][3], mat[3][3];
875
876                         /* use channels to get stats */
877                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
878                                 if (pchan->bone && pchan->bone->flag & BONE_TRANSFORM) {
879                                         add_v3_v3(normal, pchan->pose_mat[2]);
880                                         add_v3_v3(plane, pchan->pose_mat[1]);
881                                 }
882                         }
883                         negate_v3(plane);
884                         
885                         /* we need the transpose of the inverse for a normal... */
886                         copy_m3_m4(imat, ob->obmat);
887                         
888                         invert_m3_m3(mat, imat);
889                         transpose_m3(mat);
890                         mul_m3_v3(mat, normal);
891                         mul_m3_v3(mat, plane);
892                         
893                         result = ORIENTATION_EDGE;
894                 }
895         }
896         else if(ob && (ob->mode & (OB_MODE_ALL_PAINT|OB_MODE_PARTICLE_EDIT)))
897         {
898         }
899         else {
900                 /* we need the one selected object, if its not active */
901                 ob = OBACT;
902                 if(ob && !(ob->flag & SELECT)) ob = NULL;
903                 
904                 for(base= scene->base.first; base; base= base->next) {
905                         if TESTBASELIB(v3d, base) {
906                                 if(ob == NULL) { 
907                                         ob= base->object;
908                                         break;
909                                 }
910                         }
911                 }
912                 
913                 if (ob) {
914                         copy_v3_v3(normal, ob->obmat[2]);
915                         copy_v3_v3(plane, ob->obmat[1]);
916                 }
917                 result = ORIENTATION_NORMAL;
918         }
919         
920         return result;
921 }
922
923 void ED_getTransformOrientationMatrix(const bContext *C, float orientation_mat[][3], int activeOnly)
924 {
925         float normal[3]={0.0, 0.0, 0.0};
926         float plane[3]={0.0, 0.0, 0.0};
927
928         int type;
929
930         type = getTransformOrientation(C, normal, plane, activeOnly);
931
932         switch (type)
933         {
934                 case ORIENTATION_NORMAL:
935                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0)
936                         {
937                                 type = ORIENTATION_NONE;
938                         }
939                         break;
940                 case ORIENTATION_VERT:
941                         if (createSpaceNormal(orientation_mat, normal) == 0)
942                         {
943                                 type = ORIENTATION_NONE;
944                         }
945                         break;
946                 case ORIENTATION_EDGE:
947                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0)
948                         {
949                                 type = ORIENTATION_NONE;
950                         }
951                         break;
952                 case ORIENTATION_FACE:
953                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0)
954                         {
955                                 type = ORIENTATION_NONE;
956                         }
957                         break;
958         }
959
960         if (type == ORIENTATION_NONE)
961         {
962                 unit_m3(orientation_mat);
963         }
964 }