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