manually merged older changes from trunk.
[blender.git] / source / blender / editors / transform / transform_orientations.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Martin Poirier
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/transform/transform_orientations.c
26  *  \ingroup edtransform
27  */
28
29
30 #include <string.h>
31 #include <ctype.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_armature_types.h"
36 #include "DNA_curve_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_view3d_types.h"
41
42
43 #include "BKE_armature.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_editVert.h"
51 #include "BLI_utildefines.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 #include "ED_curve.h" /* for ED_curve_editnurbs */
61
62
63 #include "RNA_define.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         {
126                 BIF_selectTransformOrientation(C, ts);
127         }
128 }
129
130 TransformOrientation *createObjectSpace(bContext *C, ReportList *UNUSED(reports), char *name, int overwrite) {
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         {
147                 strncpy(name, ob->id.name+2, 35);
148         }
149
150         return addMatrixSpace(C, mat, name, overwrite); 
151 }
152
153 TransformOrientation *createBoneSpace(bContext *C, ReportList *reports, char *name, int overwrite) {
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         {
166                 strcpy(name, "Bone");
167         }
168
169         return addMatrixSpace(C, mat, name, overwrite);
170 }
171
172 TransformOrientation *createMeshSpace(bContext *C, ReportList *reports, char *name, int overwrite) {
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                         {
189                                 strcpy(name, "Vertex");
190                         }
191                         break;
192                 case ORIENTATION_EDGE:
193                         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
194                                 BKE_reports_prepend(reports, "Cannot use zero-length edge");
195                                 return NULL;
196                         }
197         
198                         if (name[0] == 0)
199                         {
200                                 strcpy(name, "Edge");
201                         }
202                         break;
203                 case ORIENTATION_FACE:
204                         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
205                                 BKE_reports_prepend(reports, "Cannot use zero-area face");
206                                 return NULL;
207                         }
208         
209                         if (name[0] == 0)
210                         {
211                                 strcpy(name, "Face");
212                         }
213                         break;
214                 default:
215                         return NULL;
216                         break;
217         }
218
219         return addMatrixSpace(C, mat, name, overwrite);
220 }
221
222 int createSpaceNormal(float mat[3][3], float normal[3])
223 {
224         float tangent[3] = {0.0f, 0.0f, 1.0f};
225         
226         VECCOPY(mat[2], normal);
227         if (normalize_v3(mat[2]) == 0.0f) {
228                 return 0; /* error return */
229         }
230
231         cross_v3_v3v3(mat[0], mat[2], tangent);
232         if (dot_v3v3(mat[0], mat[0]) == 0.0f) {
233                 tangent[0] = 1.0f;
234                 tangent[1] = tangent[2] = 0.0f;
235                 cross_v3_v3v3(mat[0], tangent, mat[2]);
236         }
237
238         cross_v3_v3v3(mat[1], mat[2], mat[0]);
239
240         normalize_m3(mat);
241         
242         return 1;
243 }
244
245 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3])
246 {
247         VECCOPY(mat[2], normal);
248         if (normalize_v3(mat[2]) == 0.0f) {
249                 return 0; /* error return */
250         }
251         
252         /* preempt zero length tangent from causing trouble */
253         if (tangent[0] == 0 && tangent[1] == 0 && tangent[2] == 0)
254         {
255                 tangent[2] = 1;
256         }
257
258         cross_v3_v3v3(mat[0], mat[2], tangent);
259         if (normalize_v3(mat[0]) == 0.0f) {
260                 return 0; /* error return */
261         }
262         
263         cross_v3_v3v3(mat[1], mat[2], mat[0]);
264
265         normalize_m3(mat);
266         
267         return 1;
268 }
269
270 TransformOrientation* addMatrixSpace(bContext *C, float mat[3][3], char name[], int overwrite) {
271         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
272         TransformOrientation *ts = NULL;
273
274         if (overwrite)
275         {
276                 ts = findOrientationName(transform_spaces, name);
277         }
278         else
279         {
280                 uniqueOrientationName(transform_spaces, name);
281         }
282
283         /* if not, create a new one */
284         if (ts == NULL)
285         {
286                 ts = MEM_callocN(sizeof(TransformOrientation), "UserTransSpace from matrix");
287                 BLI_addtail(transform_spaces, ts);
288                 strncpy(ts->name, name, 35);
289         }
290
291         /* copy matrix into transform space */
292         copy_m3_m3(ts->mat, mat);
293
294         return ts;
295 }
296
297 void BIF_removeTransformOrientation(bContext *C, TransformOrientation *target) {
298         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
299         TransformOrientation *ts;
300         int i;
301         
302         for (i = 0, ts = transform_spaces->first; ts; ts = ts->next, i++) {
303                 if (ts == target) {
304                         View3D *v3d = CTX_wm_view3d(C);
305                         if(v3d) {
306                                 int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
307                                 
308                                 // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
309                                 if (selected_index == i) {
310                                         v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
311                                 }
312                                 else if (selected_index > i) {
313                                         v3d->twmode--;
314                                 }
315                                 
316                         }
317
318                         BLI_freelinkN(transform_spaces, ts);
319                         break;
320                 }
321         }
322 }
323
324 void BIF_removeTransformOrientationIndex(bContext *C, int index) {
325         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
326         TransformOrientation *ts= BLI_findlink(transform_spaces, index);
327
328         if (ts) {
329                 View3D *v3d = CTX_wm_view3d(C);
330                 if(v3d) {
331                         int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
332                         
333                         // Transform_fix_me NEED TO DO THIS FOR ALL VIEW3D
334                         if (selected_index == index) {
335                                 v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
336                         }
337                         else if (selected_index > index) {
338                                 v3d->twmode--;
339                         }
340                         
341                 }
342
343                 BLI_freelinkN(transform_spaces, ts);
344         }
345 }
346
347 void BIF_selectTransformOrientation(bContext *C, TransformOrientation *target) {
348         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
349         View3D *v3d = CTX_wm_view3d(C);
350         TransformOrientation *ts;
351         int i;
352         
353         for (i = 0, ts = transform_spaces->first; ts; ts = ts->next, i++) {
354                 if (ts == target) {
355                         v3d->twmode = V3D_MANIP_CUSTOM + i;
356                         break;
357                 }
358         }
359 }
360
361 void BIF_selectTransformOrientationValue(bContext *C, int orientation) {
362         View3D *v3d = CTX_wm_view3d(C);
363         if(v3d) /* currently using generic poll */
364                 v3d->twmode = orientation;
365 }
366
367 EnumPropertyItem *BIF_enumTransformOrientation(bContext *C)
368 {
369         Scene *scene;
370         ListBase *transform_spaces;
371         TransformOrientation *ts= NULL;
372
373         EnumPropertyItem global = {V3D_MANIP_GLOBAL, "GLOBAL", 0, "Global", ""};
374         EnumPropertyItem normal = {V3D_MANIP_NORMAL, "NORMAL", 0, "Normal", ""};
375         EnumPropertyItem local = {V3D_MANIP_LOCAL, "LOCAL", 0, "Local", ""};
376         EnumPropertyItem view = {V3D_MANIP_VIEW, "VIEW", 0, "View", ""};
377         EnumPropertyItem tmp = {0, "", 0, "", ""};
378         EnumPropertyItem *item= NULL;
379         int i = V3D_MANIP_CUSTOM, totitem= 0;
380
381         RNA_enum_item_add(&item, &totitem, &global);
382         RNA_enum_item_add(&item, &totitem, &normal);
383         RNA_enum_item_add(&item, &totitem, &local);
384         RNA_enum_item_add(&item, &totitem, &view);
385
386         if(C) {
387                 scene= CTX_data_scene(C);
388
389                 if(scene) {
390                         transform_spaces = &scene->transform_spaces;
391                         ts = transform_spaces->first;
392                 }
393         }
394                 
395         if(ts)
396                 RNA_enum_item_add_separator(&item, &totitem);
397
398         for(; ts; ts = ts->next) {
399                 tmp.identifier = "CUSTOM";
400                 tmp.name= ts->name;
401                 tmp.value = i++;
402                 RNA_enum_item_add(&item, &totitem, &tmp);
403         }
404
405         RNA_enum_item_end(&item, &totitem);
406
407         return item;
408 }
409
410 const char * BIF_menustringTransformOrientation(const bContext *C, const char *title) {
411         char menu[] = "%t|Global%x0|Local%x1|Gimbal%x4|Normal%x2|View%x3";
412         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
413         TransformOrientation *ts;
414         int i = V3D_MANIP_CUSTOM;
415         char *str_menu, *p;
416         
417         
418         str_menu = MEM_callocN(strlen(menu) + strlen(title) + 1 + 40 * BIF_countTransformOrientation(C), "UserTransSpace from matrix");
419         p = str_menu;
420         
421         p += sprintf(str_menu, "%s", title);
422         p += sprintf(p, "%s", menu);
423         
424         for (ts = transform_spaces->first; ts; ts = ts->next) {
425                 p += sprintf(p, "|%s%%x%d", ts->name, i++);
426         }
427         
428         return str_menu;
429 }
430
431 int BIF_countTransformOrientation(const bContext *C) {
432         ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
433         TransformOrientation *ts;
434         int count = 0;
435
436         for (ts = transform_spaces->first; ts; ts = ts->next) {
437                 count++;
438         }
439         
440         return count;
441 }
442
443 void applyTransformOrientation(const bContext *C, float mat[3][3], char *name) {
444         TransformOrientation *ts;
445         View3D *v3d = CTX_wm_view3d(C);
446         int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
447         int i;
448         
449         if (selected_index >= 0) {
450                 for (i = 0, ts = CTX_data_scene(C)->transform_spaces.first; ts; ts = ts->next, i++) {
451                         if (selected_index == i) {
452                                 
453                                 if (name)
454                                         strcpy(name, ts->name);
455                                 
456                                 copy_m3_m3(mat, ts->mat);
457                                 break;
458                         }
459                 }
460           }
461 }
462
463 static int count_bone_select(bArmature *arm, ListBase *lb, int do_it) 
464 {
465         Bone *bone;
466         int do_next;
467         int total = 0;
468         
469         for(bone= lb->first; bone; bone= bone->next) {
470                 bone->flag &= ~BONE_TRANSFORM;
471                 do_next = do_it;
472                 if(do_it) {
473                         if(bone->layer & arm->layer) {
474                                 if (bone->flag & BONE_SELECTED) {
475                                         bone->flag |= BONE_TRANSFORM;
476                                         total++;
477                                         do_next= 0;     // no transform on children if one parent bone is selected
478                                 }
479                         }
480                 }
481                 total += count_bone_select(arm, &bone->childbase, do_next);
482         }
483         
484         return total;
485 }
486
487 void initTransformOrientation(bContext *C, TransInfo *t)
488 {
489         View3D *v3d = CTX_wm_view3d(C);
490         Object *ob = CTX_data_active_object(C);
491         Object *obedit = CTX_data_active_object(C);
492
493         switch(t->current_orientation) {
494         case V3D_MANIP_GLOBAL:
495                 unit_m3(t->spacemtx);
496                 strcpy(t->spacename, "global");
497                 break;
498
499         case V3D_MANIP_GIMBAL:
500                 unit_m3(t->spacemtx);
501                 if (gimbal_axis(ob, t->spacemtx)) {
502                         strcpy(t->spacename, "gimbal");
503                         break;
504                 }
505                 /* no gimbal fallthrough to normal */
506         case V3D_MANIP_NORMAL:
507                 if(obedit || (ob && ob->mode & OB_MODE_POSE)) {
508                         strcpy(t->spacename, "normal");
509                         ED_getTransformOrientationMatrix(C, t->spacemtx, (v3d->around == V3D_ACTIVE));
510                         break;
511                 }
512                 /* no break we define 'normal' as 'local' in Object mode */
513         case V3D_MANIP_LOCAL:
514                 strcpy(t->spacename, "local");
515                 
516                 if(ob) {
517                         copy_m3_m4(t->spacemtx, ob->obmat);
518                         normalize_m3(t->spacemtx);
519                 } else {
520                         unit_m3(t->spacemtx);
521                 }
522                 
523                 break;
524                 
525         case V3D_MANIP_VIEW:
526                 if (t->ar->regiontype == RGN_TYPE_WINDOW)
527                 {
528                         RegionView3D *rv3d = t->ar->regiondata;
529                         float mat[3][3];
530
531                         strcpy(t->spacename, "view");
532                         copy_m3_m4(mat, rv3d->viewinv);
533                         normalize_m3(mat);
534                         copy_m3_m3(t->spacemtx, mat);
535                 }
536                 else
537                 {
538                         unit_m3(t->spacemtx);
539                 }
540                 break;
541         default: /* V3D_MANIP_CUSTOM */
542                 applyTransformOrientation(C, t->spacemtx, t->spacename);
543                 break;
544         }
545 }
546
547 int getTransformOrientation(const bContext *C, float normal[3], float plane[3], int activeOnly)
548 {
549         Scene *scene = CTX_data_scene(C);
550         View3D *v3d = CTX_wm_view3d(C);
551         Object *obedit= CTX_data_edit_object(C);
552         Base *base;
553         Object *ob = OBACT;
554         int result = ORIENTATION_NONE;
555
556         normal[0] = normal[1] = normal[2] = 0;
557         plane[0] = plane[1] = plane[2] = 0;
558
559         if(obedit)
560         {
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                 {
573                         Mesh *me= ob->data;
574                         BMEditMesh *em = me->edit_btmesh;
575                         BMVert *eve;
576                         BMEditSelection ese;
577                         float vec[3]= {0,0,0};
578                         
579                         /* USE LAST SELECTED WITH ACTIVE */
580                         if (activeOnly && EDBM_get_actSelection(em, &ese))
581                         {
582                                 EDBM_editselection_normal(normal, &ese);
583                                 EDBM_editselection_plane(em, plane, &ese);
584                                 
585                                 switch (ese.type)
586                                 {
587                                         case BM_VERT:
588                                                 result = ORIENTATION_VERT;
589                                                 break;
590                                         case BM_EDGE:
591                                                 result = ORIENTATION_EDGE;
592                                                 break;
593                                         case BM_FACE:
594                                                 result = ORIENTATION_FACE;
595                                                 break;
596                                 }
597                         }
598                         else
599                         {
600                                 if (em->bm->totfacesel >= 1)
601                                 {
602                                         BMFace *efa;
603                                         BMIter iter;
604
605                                         BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
606                                                 if(BM_TestHFlag(efa, BM_SELECT)) {
607                                                         VECADD(normal, normal, efa->no);
608                                                         sub_v3_v3v3(vec, ((BMLoopList*)efa->loops.first)->first->v->co, 
609                                                                 (((BMLoopList*)efa->loops.first)->first->next)->v->co);
610                                                         VECADD(plane, plane, vec);
611                                                 }
612                                         }
613                                         
614                                         result = ORIENTATION_FACE;
615                                 }
616                                 else if (em->bm->totvertsel == 3)
617                                 {
618                                         BMVert *v1 = NULL, *v2 = NULL, *v3 = NULL;
619                                         BMIter iter;
620                                         float cotangent[3];
621                                         
622                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
623                                                 if (BM_TestHFlag(eve, BM_SELECT)) {
624                                                         if (v1 == NULL) {
625                                                                 v1 = eve; 
626                                                         }
627                                                         else if (v2 == NULL) {
628                                                                 v2 = eve;
629                                                         }
630                                                         else {
631                                                                 v3 = eve;
632
633                                                                 sub_v3_v3v3(plane, v2->co, v1->co);
634                                                                 sub_v3_v3v3(cotangent, v3->co, v2->co);
635                                                                 cross_v3_v3v3(normal, cotangent, plane);
636                                                                 break;
637                                                         }
638                                                 }
639                                         }
640
641                                         /* if there's an edge available, use that for the tangent */
642                                         if (em->bm->totedgesel >= 1)
643                                         {
644                                                 BMEdge *eed = NULL;
645                                                 BMIter iter;
646                                                 
647                                                 BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
648                                                         if(BM_TestHFlag(eed, BM_SELECT)) {
649                                                                 sub_v3_v3v3(plane, eed->v2->co, eed->v1->co);
650                                                                 break;
651                                                         }
652                                                 }
653                                         }
654
655                                         result = ORIENTATION_FACE;
656                                 }
657                                 else if (em->bm->totedgesel == 1)
658                                 {
659                                         BMEdge *eed = NULL;
660                                         BMIter iter;
661                                         
662                                         BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
663                                                 if(BM_TestHFlag(eed, BM_SELECT)) {
664                                                         /* use average vert normals as plane and edge vector as normal */
665                                                         VECCOPY(plane, eed->v1->no);
666                                                         VECADD(plane, plane, eed->v2->no);
667                                                         sub_v3_v3v3(normal, eed->v2->co, eed->v1->co);
668                                                         break;
669                                                 }
670                                         }
671                                         result = ORIENTATION_EDGE;
672                                 }
673                                 else if (em->bm->totvertsel == 2)
674                                 {
675                                         BMVert *v1 = NULL, *v2 = NULL;
676                                         BMIter iter;
677
678                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
679                                                 if (BM_TestHFlag(eve, BM_SELECT)) {
680                                                         if (v1 == NULL) {
681                                                                 v1 = eve; 
682                                                         }
683                                                         else {
684                                                                 v2 = eve;
685                                                                 
686                                                                 VECCOPY(plane, v1->no);
687                                                                 VECADD(plane, plane, v2->no);
688                                                                 sub_v3_v3v3(normal, v2->co, v1->co);
689                                                                 break; 
690                                                         }
691                                                 }
692                                         }
693                                         result = ORIENTATION_EDGE;
694                                 }
695                                 else if (em->bm->totvertsel == 1)
696                                 {
697                                         BMIter iter;
698
699                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
700                                                 if (BM_TestHFlag(eve, BM_SELECT)) {
701                                                         VECCOPY(normal, eve->no);
702                                                         break;
703                                                 }
704                                         }
705                                         result = ORIENTATION_VERT;
706                                 }
707                                 else if (em->bm->totvertsel > 3)
708                                 {
709                                         BMIter iter;
710                                         normal[0] = normal[1] = normal[2] = 0.0f;
711
712                                         BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
713                                                 if (BM_TestHFlag(eve, BM_SELECT)) {
714                                                         add_v3_v3(normal, eve->no);
715                                                 }
716                                         }
717                                         normalize_v3(normal);
718                                         result = ORIENTATION_VERT;
719                                 }
720                         }
721                 } /* end editmesh */
722                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
723                 {
724                         Curve *cu= obedit->data;
725                         Nurb *nu;
726                         BezTriple *bezt;
727                         int a;
728                         ListBase *nurbs= ED_curve_editnurbs(cu);
729
730                         for (nu = nurbs->first; nu; nu = nu->next)
731                         {
732                                 /* only bezier has a normal */
733                                 if(nu->type == CU_BEZIER)
734                                 {
735                                         bezt= nu->bezt;
736                                         a= nu->pntsu;
737                                         while(a--)
738                                         {
739                                                 /* exception */
740                                                 if ( (bezt->f1 & SELECT) + (bezt->f2 & SELECT) + (bezt->f3 & SELECT) > SELECT )
741                                                 {
742                                                         sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[2]);
743                                                 }
744                                                 else
745                                                 {
746                                                         if(bezt->f1)
747                                                         {
748                                                                 sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[1]);
749                                                         }
750                                                         if(bezt->f2)
751                                                         {
752                                                                 sub_v3_v3v3(normal, bezt->vec[0], bezt->vec[2]);
753                                                         }
754                                                         if(bezt->f3)
755                                                         {
756                                                                 sub_v3_v3v3(normal, bezt->vec[1], bezt->vec[2]);
757                                                         }
758                                                 }
759                                                 bezt++;
760                                         }
761                                 }
762                         }
763                         
764                         if (normal[0] != 0 || normal[1] != 0 || normal[2] != 0)
765                         {
766                                 result = ORIENTATION_NORMAL;
767                         }
768                 }
769                 else if(obedit->type==OB_MBALL)
770                 {
771 #if 0 // XXX
772                         /* editmball.c */
773                         extern ListBase editelems;  /* go away ! */
774                         MetaElem *ml, *ml_sel = NULL;
775         
776                         /* loop and check that only one element is selected */  
777                         for (ml = editelems.first; ml; ml = ml->next)
778                         {
779                                 if (ml->flag & SELECT) {
780                                         if (ml_sel == NULL)
781                                         {
782                                                 ml_sel = ml;
783                                         }
784                                         else
785                                         {
786                                                 ml_sel = NULL;
787                                                 break;
788                                         }
789                                 }
790                         }
791                         
792                         if (ml_sel)
793                         {       
794                                 float mat[4][4];
795
796                                 /* Rotation of MetaElem is stored in quat */
797                                  quat_to_mat4( mat,ml_sel->quat);
798
799                                 VECCOPY(normal, mat[2]);
800
801                                 negate_v3_v3(plane, mat[1]);
802                                 
803                                 result = ORIENTATION_NORMAL;
804                         }
805 #endif
806                         
807                 }
808                 else if (obedit->type == OB_ARMATURE)
809                 {
810                         bArmature *arm = obedit->data;
811                         EditBone *ebone;
812                         
813                         for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
814                                 if (arm->layer & ebone->layer)
815                                 {
816                                         if (ebone->flag & BONE_SELECTED)
817                                         {
818                                                 float tmat[3][3];
819                                                 float vec[3];
820                                                 sub_v3_v3v3(vec, ebone->tail, ebone->head);
821                                                 normalize_v3(vec);
822                                                 add_v3_v3(normal, vec);
823                                                 
824                                                 vec_roll_to_mat3(vec, ebone->roll, tmat);
825                                                 add_v3_v3(plane, tmat[2]);
826                                         }
827                                 }
828                         }
829                         
830                         normalize_v3(normal);
831                         normalize_v3(plane);
832
833                         if (plane[0] != 0 || plane[1] != 0 || plane[2] != 0)
834                         {
835                                 result = ORIENTATION_EDGE;
836                         }
837
838                 }
839
840                 /* Vectors from edges don't need the special transpose inverse multiplication */
841                 if (result == ORIENTATION_EDGE)
842                 {
843                         mul_mat3_m4_v3(ob->obmat, normal);
844                         mul_mat3_m4_v3(ob->obmat, plane);
845                 }
846                 else
847                 {
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                         VECCOPY(normal, ob->obmat[2]);
901                         VECCOPY(plane, ob->obmat[1]);
902                 }
903                 else {
904                         normal[0] = 0.0f;
905                         normal[1] = 0.0f;
906                         normal[2] = 1.0f;
907                         plane[0] = 1.0f;
908                         plane[1] = 0.0f;
909                         plane[2] = 0.0f;
910                 }
911                 result = ORIENTATION_NORMAL;
912         }
913         
914         return result;
915 }
916
917 void ED_getTransformOrientationMatrix(const bContext *C, float orientation_mat[][3], int activeOnly)
918 {
919         float normal[3]={0.0, 0.0, 0.0};
920         float plane[3]={0.0, 0.0, 0.0};
921
922         int type;
923
924         type = getTransformOrientation(C, normal, plane, activeOnly);
925
926         switch (type)
927         {
928                 case ORIENTATION_NORMAL:
929                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0)
930                         {
931                                 type = ORIENTATION_NONE;
932                         }
933                         break;
934                 case ORIENTATION_VERT:
935                         if (createSpaceNormal(orientation_mat, normal) == 0)
936                         {
937                                 type = ORIENTATION_NONE;
938                         }
939                         break;
940                 case ORIENTATION_EDGE:
941                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0)
942                         {
943                                 type = ORIENTATION_NONE;
944                         }
945                         break;
946                 case ORIENTATION_FACE:
947                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0)
948                         {
949                                 type = ORIENTATION_NONE;
950                         }
951                         break;
952         }
953
954         if (type == ORIENTATION_NONE)
955         {
956                 unit_m3(orientation_mat);
957         }
958 }