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