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