Context operators for adjusting context values directly to avoid adding operators...
[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 #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|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 void initTransformOrientation(bContext *C, TransInfo *t)
514 {
515         View3D *v3d = CTX_wm_view3d(C);
516         Object *ob = CTX_data_active_object(C);
517         Object *obedit = CTX_data_active_object(C);
518         float normal[3]={0.0, 0.0, 0.0};
519         float plane[3]={0.0, 0.0, 0.0};
520
521         switch(t->current_orientation) {
522         case V3D_MANIP_GLOBAL:
523                 strcpy(t->spacename, "global");
524                 break;
525                 
526         case V3D_MANIP_NORMAL:
527                 if(obedit || ob->mode & OB_MODE_POSE) {
528                         float mat[3][3];
529                         int type;
530                         
531                         strcpy(t->spacename, "normal");
532                         
533                         type = getTransformOrientation(C, normal, plane, (v3d->around == V3D_ACTIVE));
534                         
535                         switch (type)
536                         {
537                                 case ORIENTATION_NORMAL:
538                                         if (createSpaceNormalTangent(mat, normal, plane) == 0)
539                                         {
540                                                 type = ORIENTATION_NONE;
541                                         }
542                                         break;
543                                 case ORIENTATION_VERT:
544                                         if (createSpaceNormal(mat, normal) == 0)
545                                         {
546                                                 type = ORIENTATION_NONE;
547                                         }
548                                         break;
549                                 case ORIENTATION_EDGE:
550                                         if (createSpaceNormalTangent(mat, normal, plane) == 0)
551                                         {
552                                                 type = ORIENTATION_NONE;
553                                         }
554                                         break;
555                                 case ORIENTATION_FACE:
556                                         if (createSpaceNormalTangent(mat, normal, plane) == 0)
557                                         {
558                                                 type = ORIENTATION_NONE;
559                                         }
560                                         break;
561                         }
562                         
563                         if (type == ORIENTATION_NONE)
564                         {
565                                 Mat3One(t->spacemtx);
566                         }
567                         else
568                         {
569                                 Mat3CpyMat3(t->spacemtx, mat);
570                         }
571                         break;
572                 }
573                 /* no break we define 'normal' as 'local' in Object mode */
574         case V3D_MANIP_LOCAL:
575                 strcpy(t->spacename, "local");
576                 Mat3CpyMat4(t->spacemtx, ob->obmat);
577                 Mat3Ortho(t->spacemtx);
578                 break;
579                 
580         case V3D_MANIP_VIEW:
581                 if (t->ar->regiontype == RGN_TYPE_WINDOW)
582                 {
583                         RegionView3D *rv3d = t->ar->regiondata;
584                         float mat[3][3];
585
586                         strcpy(t->spacename, "view");
587                         Mat3CpyMat4(mat, rv3d->viewinv);
588                         Mat3Ortho(mat);
589                         Mat3CpyMat3(t->spacemtx, mat);
590                 }
591                 else
592                 {
593                         Mat3One(t->spacemtx);
594                 }
595                 break;
596         default: /* V3D_MANIP_CUSTOM */
597                 applyTransformOrientation(C, t->spacemtx, t->spacename);
598                 break;
599         }
600 }
601
602 int getTransformOrientation(const bContext *C, float normal[3], float plane[3], int activeOnly)
603 {
604         Scene *scene = CTX_data_scene(C);
605         View3D *v3d = CTX_wm_view3d(C);
606         Object *obedit= CTX_data_edit_object(C);
607         Base *base;
608         Object *ob = OBACT;
609         int result = ORIENTATION_NONE;
610
611         normal[0] = normal[1] = normal[2] = 0;
612         plane[0] = plane[1] = plane[2] = 0;
613
614         if(obedit)
615         {
616                 float imat[3][3], mat[3][3];
617                 
618                 /* we need the transpose of the inverse for a normal... */
619                 Mat3CpyMat4(imat, ob->obmat);
620                 
621                 Mat3Inv(mat, imat);
622                 Mat3Transp(mat);
623
624                 ob= obedit;
625
626                 if(ob->type==OB_MESH)
627                 {
628                         Mesh *me= ob->data;
629                         EditMesh *em = me->edit_mesh;
630                         EditVert *eve;
631                         EditSelection ese;
632                         float vec[3]= {0,0,0};
633                         
634                         /* USE LAST SELECTED WITH ACTIVE */
635                         if (activeOnly && EM_get_actSelection(em, &ese))
636                         {
637                                 EM_editselection_normal(normal, &ese);
638                                 EM_editselection_plane(plane, &ese);
639                                 
640                                 switch (ese.type)
641                                 {
642                                         case EDITVERT:
643                                                 result = ORIENTATION_VERT;
644                                                 break;
645                                         case EDITEDGE:
646                                                 result = ORIENTATION_EDGE;
647                                                 break;
648                                         case EDITFACE:
649                                                 result = ORIENTATION_FACE;
650                                                 break;
651                                 }
652                         }
653                         else
654                         {
655                                 if (em->totfacesel >= 1)
656                                 {
657                                         EditFace *efa;
658                                         
659                                         for(efa= em->faces.first; efa; efa= efa->next)
660                                         {
661                                                 if(efa->f & SELECT)
662                                                 {
663                                                         VECADD(normal, normal, efa->n);
664                                                         VecSubf(vec, efa->v2->co, efa->v1->co);
665                                                         VECADD(plane, plane, vec);
666                                                 }
667                                         }
668                                         
669                                         result = ORIENTATION_FACE;
670                                 }
671                                 else if (em->totvertsel == 3)
672                                 {
673                                         EditVert *v1 = NULL, *v2 = NULL, *v3 = NULL;
674                                         float cotangent[3];
675                                         
676                                         for (eve = em->verts.first; eve; eve = eve->next)
677                                         {
678                                                 if ( eve->f & SELECT ) {
679                                                         if (v1 == NULL) {
680                                                                 v1 = eve; 
681                                                         }
682                                                         else if (v2 == NULL) {
683                                                                 v2 = eve;
684                                                         }
685                                                         else {
686                                                                 v3 = eve;
687
688                                                                 VecSubf(plane, v2->co, v1->co);
689                                                                 VecSubf(cotangent, v3->co, v2->co);
690                                                                 Crossf(normal, cotangent, plane);
691                                                                 break;
692                                                         }
693                                                 }
694                                         }
695
696                                         /* if there's an edge available, use that for the tangent */
697                                         if (em->totedgesel >= 1)
698                                         {
699                                                 EditEdge *eed = NULL;
700         
701                                                 for(eed= em->edges.first; eed; eed= eed->next) {
702                                                         if(eed->f & SELECT) {
703                                                                 VecSubf(plane, eed->v2->co, eed->v1->co);
704                                                                 break;
705                                                         }
706                                                 }
707                                         }
708
709                                         result = ORIENTATION_FACE;
710                                 }
711                                 else if (em->totedgesel == 1)
712                                 {
713                                         EditEdge *eed;
714
715                                         for(eed= em->edges.first; eed; eed= eed->next) {
716                                                 if(eed->f & SELECT) {
717                                                         /* use average vert normals as plane and edge vector as normal */
718                                                         VECCOPY(plane, eed->v1->no);
719                                                         VECADD(plane, plane, eed->v2->no);
720                                                         VecSubf(normal, eed->v2->co, eed->v1->co);
721                                                         break;
722                                                 }
723                                         }
724                                         result = ORIENTATION_EDGE;
725                                 }
726                                 else if (em->totvertsel == 2)
727                                 {
728                                         EditVert *v1 = NULL, *v2 = NULL;
729                 
730                                         for (eve = em->verts.first; eve; eve = eve->next)
731                                         {
732                                                 if ( eve->f & SELECT ) {
733                                                         if (v1 == NULL) {
734                                                                 v1 = eve; 
735                                                         }
736                                                         else {
737                                                                 v2 = eve;
738                                                                 
739                                                                 VECCOPY(plane, v1->no);
740                                                                 VECADD(plane, plane, v2->no);
741                                                                 VecSubf(normal, v2->co, v1->co);
742                                                                 break; 
743                                                         }
744                                                 }
745                                         }
746                                         result = ORIENTATION_EDGE;
747                                 }
748                                 else if (em->totvertsel == 1)
749                                 {
750                                         for (eve = em->verts.first; eve; eve = eve->next)
751                                         {
752                                                 if ( eve->f & SELECT ) {
753                                                         VECCOPY(normal, eve->no);
754                                                         break;
755                                                 }
756                                         }
757                                         result = ORIENTATION_VERT;
758                                 }
759                                 else if (em->totvertsel > 3)
760                                 {
761                                         normal[0] = normal[1] = normal[2] = 0;
762                                         
763                                         for (eve = em->verts.first; eve; eve = eve->next)
764                                         {
765                                                 if ( eve->f & SELECT ) {
766                                                         VecAddf(normal, normal, eve->no);
767                                                 }
768                                         }
769                                         Normalize(normal);
770                                         result = ORIENTATION_VERT;
771                                 }
772                         }
773                 } /* end editmesh */
774                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
775                 {
776                         Curve *cu= obedit->data;
777                         Nurb *nu;
778                         BezTriple *bezt;
779                         int a;
780                         
781                         for (nu = cu->editnurb->first; nu; nu = nu->next)
782                         {
783                                 /* only bezier has a normal */
784                                 if(nu->type == CU_BEZIER)
785                                 {
786                                         bezt= nu->bezt;
787                                         a= nu->pntsu;
788                                         while(a--)
789                                         {
790                                                 /* exception */
791                                                 if ( (bezt->f1 & SELECT) + (bezt->f2 & SELECT) + (bezt->f3 & SELECT) > SELECT )
792                                                 {
793                                                         VecSubf(normal, bezt->vec[0], bezt->vec[2]);
794                                                 }
795                                                 else
796                                                 {
797                                                         if(bezt->f1)
798                                                         {
799                                                                 VecSubf(normal, bezt->vec[0], bezt->vec[1]);
800                                                         }
801                                                         if(bezt->f2)
802                                                         {
803                                                                 VecSubf(normal, bezt->vec[0], bezt->vec[2]);
804                                                         }
805                                                         if(bezt->f3)
806                                                         {
807                                                                 VecSubf(normal, bezt->vec[1], bezt->vec[2]);
808                                                         }
809                                                 }
810                                                 bezt++;
811                                         }
812                                 }
813                         }
814                         
815                         if (normal[0] != 0 || normal[1] != 0 || normal[2] != 0)
816                         {
817                                 result = ORIENTATION_NORMAL;
818                         }
819                 }
820                 else if(obedit->type==OB_MBALL)
821                 {
822 #if 0 // XXX
823                         /* editmball.c */
824                         extern ListBase editelems;  /* go away ! */
825                         MetaElem *ml, *ml_sel = NULL;
826         
827                         /* loop and check that only one element is selected */  
828                         for (ml = editelems.first; ml; ml = ml->next)
829                         {
830                                 if (ml->flag & SELECT) {
831                                         if (ml_sel == NULL)
832                                         {
833                                                 ml_sel = ml;
834                                         }
835                                         else
836                                         {
837                                                 ml_sel = NULL;
838                                                 break;
839                                         }
840                                 }
841                         }
842                         
843                         if (ml_sel)
844                         {       
845                                 float mat[4][4];
846
847                                 /* Rotation of MetaElem is stored in quat */
848                                 QuatToMat4(ml_sel->quat, mat);
849
850                                 VECCOPY(normal, mat[2]);
851                                 VECCOPY(plane, mat[1]);
852
853                                 VecMulf(plane, -1.0);
854                                 
855                                 result = ORIENTATION_NORMAL;
856                         }
857 #endif
858                         
859                 }
860                 else if (obedit->type == OB_ARMATURE)
861                 {
862                         bArmature *arm = obedit->data;
863                         EditBone *ebone;
864                         
865                         for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
866                                 if (arm->layer & ebone->layer)
867                                 {
868                                         if (ebone->flag & BONE_SELECTED)
869                                         {
870                                                 float mat[3][3];
871                                                 float vec[3];
872                                                 VecSubf(vec, ebone->tail, ebone->head);
873                                                 Normalize(vec);
874                                                 VecAddf(normal, normal, vec);
875                                                 
876                                                 vec_roll_to_mat3(vec, ebone->roll, mat);
877                                                 VecAddf(plane, plane, mat[2]);
878                                         }
879                                 }
880                         }
881                         
882                         Normalize(normal);
883                         Normalize(plane);
884
885                         if (plane[0] != 0 || plane[1] != 0 || plane[2] != 0)
886                         {
887                                 result = ORIENTATION_EDGE;
888                         }
889
890                 }
891
892                 /* Vectors from edges don't need the special transpose inverse multiplication */
893                 if (result == ORIENTATION_EDGE)
894                 {
895                         Mat4Mul3Vecfl(ob->obmat, normal);
896                         Mat4Mul3Vecfl(ob->obmat, plane);
897                 }
898                 else
899                 {
900                         Mat3MulVecfl(mat, normal);
901                         Mat3MulVecfl(mat, plane);
902                 }
903         }
904         else if(ob && (ob->mode & OB_MODE_POSE))
905         {
906                 bArmature *arm= ob->data;
907                 bPoseChannel *pchan;
908                 int totsel;
909                 
910                 totsel = count_bone_select(arm, &arm->bonebase, 1);
911                 if(totsel) {
912                         float imat[3][3], mat[3][3];
913
914                         /* use channels to get stats */
915                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
916                                 if (pchan->bone && pchan->bone->flag & BONE_TRANSFORM) {
917                                         VecAddf(normal, normal, pchan->pose_mat[2]);
918                                         VecAddf(plane, plane, pchan->pose_mat[1]);
919                                 }
920                         }
921                         VecMulf(plane, -1.0);
922                         
923                         /* we need the transpose of the inverse for a normal... */
924                         Mat3CpyMat4(imat, ob->obmat);
925                         
926                         Mat3Inv(mat, imat);
927                         Mat3Transp(mat);
928                         Mat3MulVecfl(mat, normal);
929                         Mat3MulVecfl(mat, plane);
930                         
931                         result = ORIENTATION_EDGE;
932                 }
933         }
934         else if(ob && (ob->mode & (OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT|OB_MODE_PARTICLE_EDIT)))
935         {
936         }
937         else {
938                 /* we need the one selected object, if its not active */
939                 ob = OBACT;
940                 if(ob && !(ob->flag & SELECT)) ob = NULL;
941                 
942                 for(base= scene->base.first; base; base= base->next) {
943                         if TESTBASELIB(v3d, base) {
944                                 if(ob == NULL) { 
945                                         ob= base->object;
946                                         break;
947                                 }
948                         }
949                 }
950                 
951                 VECCOPY(normal, ob->obmat[2]);
952                 VECCOPY(plane, ob->obmat[1]);
953                 result = ORIENTATION_NORMAL;
954         }
955         
956         return result;
957 }