~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/editors/transform/transform_orientations.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

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