9e6f4847b5bc5282d83b7ba30c146017dc62ee4d
[blender.git] / source / blender / editors / transform / transform_orientations.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Martin Poirier
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/transform/transform_orientations.c
24  *  \ingroup edtransform
25  */
26
27 #include <string.h>
28 #include <stddef.h>
29 #include <ctype.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_armature_types.h"
34 #include "DNA_curve_types.h"
35 #include "DNA_meta_types.h"
36 #include "DNA_object_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 #include "DNA_workspace_types.h"
42
43 #include "BLI_math.h"
44 #include "BLI_listbase.h"
45 #include "BLI_string.h"
46 #include "BLI_string_utils.h"
47 #include "BLI_utildefines.h"
48
49 #include "BKE_action.h"
50 #include "BKE_curve.h"
51 #include "BKE_context.h"
52 #include "BKE_editmesh.h"
53 #include "BKE_report.h"
54 #include "BKE_main.h"
55 #include "BKE_screen.h"
56 #include "BKE_scene.h"
57 #include "BKE_workspace.h"
58
59 #include "BLT_translation.h"
60
61 #include "ED_armature.h"
62
63 #include "transform.h"
64
65 /* *********************** TransSpace ************************** */
66
67 void BIF_clearTransformOrientation(bContext *C)
68 {
69         Scene *scene = CTX_data_scene(C);
70         ListBase *transform_orientations = &scene->transform_spaces;
71         View3D *v3d = CTX_wm_view3d(C);
72
73         BLI_freelistN(transform_orientations);
74
75         if (v3d && scene->orientation_type == V3D_MANIP_CUSTOM) {
76                 scene->orientation_type = V3D_MANIP_GLOBAL; /* fallback to global */
77                 scene->orientation_index_custom = -1;
78         }
79 }
80
81 static TransformOrientation *findOrientationName(ListBase *lb, const char *name)
82 {
83         return BLI_findstring(lb, name, offsetof(TransformOrientation, name));
84 }
85
86 static bool uniqueOrientationNameCheck(void *arg, const char *name)
87 {
88         return findOrientationName((ListBase *)arg, name) != NULL;
89 }
90
91 static void uniqueOrientationName(ListBase *lb, char *name)
92 {
93         BLI_uniquename_cb(uniqueOrientationNameCheck, lb, CTX_DATA_(BLT_I18NCONTEXT_ID_SCENE, "Space"), '.', name,
94                           sizeof(((TransformOrientation *)NULL)->name));
95 }
96
97 static TransformOrientation *createViewSpace(bContext *C, ReportList *UNUSED(reports),
98                                              const char *name, const bool overwrite)
99 {
100         RegionView3D *rv3d = CTX_wm_region_view3d(C);
101         float mat[3][3];
102
103         if (!rv3d)
104                 return NULL;
105
106         copy_m3_m4(mat, rv3d->viewinv);
107         normalize_m3(mat);
108
109         if (name[0] == 0) {
110                 View3D *v3d = CTX_wm_view3d(C);
111                 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
112                         /* If an object is used as camera, then this space is the same as object space! */
113                         name = v3d->camera->id.name + 2;
114                 }
115                 else {
116                         name = "Custom View";
117                 }
118         }
119
120         return addMatrixSpace(C, mat, name, overwrite);
121 }
122
123 static TransformOrientation *createObjectSpace(bContext *C, ReportList *UNUSED(reports),
124                                                const char *name, const bool overwrite)
125 {
126         Base *base = CTX_data_active_base(C);
127         Object *ob;
128         float mat[3][3];
129
130         if (base == NULL)
131                 return NULL;
132
133         ob = base->object;
134
135         copy_m3_m4(mat, ob->obmat);
136         normalize_m3(mat);
137
138         /* use object name if no name is given */
139         if (name[0] == 0) {
140                 name = ob->id.name + 2;
141         }
142
143         return addMatrixSpace(C, mat, name, overwrite);
144 }
145
146 static TransformOrientation *createBoneSpace(bContext *C, ReportList *reports,
147                                              const char *name, const bool overwrite)
148 {
149         float mat[3][3];
150         float normal[3], plane[3];
151
152         getTransformOrientation(C, normal, plane);
153
154         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
155                 BKE_reports_prepend(reports, "Cannot use zero-length bone");
156                 return NULL;
157         }
158
159         if (name[0] == 0) {
160                 name = "Bone";
161         }
162
163         return addMatrixSpace(C, mat, name, overwrite);
164 }
165
166 static TransformOrientation *createCurveSpace(bContext *C, ReportList *reports,
167                                               const char *name, const bool overwrite)
168 {
169         float mat[3][3];
170         float normal[3], plane[3];
171
172         getTransformOrientation(C, normal, plane);
173
174         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
175                 BKE_reports_prepend(reports, "Cannot use zero-length curve");
176                 return NULL;
177         }
178
179         if (name[0] == 0) {
180                 name = "Curve";
181         }
182
183         return addMatrixSpace(C, mat, name, overwrite);
184 }
185
186
187 static TransformOrientation *createMeshSpace(bContext *C, ReportList *reports,
188                                              const char *name, const bool overwrite)
189 {
190         float mat[3][3];
191         float normal[3], plane[3];
192         int type;
193
194         type = getTransformOrientation(C, normal, plane);
195         
196         switch (type) {
197                 case ORIENTATION_VERT:
198                         if (createSpaceNormal(mat, normal) == 0) {
199                                 BKE_reports_prepend(reports, "Cannot use vertex with zero-length normal");
200                                 return NULL;
201                         }
202         
203                         if (name[0] == 0) {
204                                 name = "Vertex";
205                         }
206                         break;
207                 case ORIENTATION_EDGE:
208                         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
209                                 BKE_reports_prepend(reports, "Cannot use zero-length edge");
210                                 return NULL;
211                         }
212         
213                         if (name[0] == 0) {
214                                 name = "Edge";
215                         }
216                         break;
217                 case ORIENTATION_FACE:
218                         if (createSpaceNormalTangent(mat, normal, plane) == 0) {
219                                 BKE_reports_prepend(reports, "Cannot use zero-area face");
220                                 return NULL;
221                         }
222         
223                         if (name[0] == 0) {
224                                 name = "Face";
225                         }
226                         break;
227                 default:
228                         return NULL;
229         }
230
231         return addMatrixSpace(C, mat, name, overwrite);
232 }
233
234 bool createSpaceNormal(float mat[3][3], const float normal[3])
235 {
236         float tangent[3] = {0.0f, 0.0f, 1.0f};
237         
238         copy_v3_v3(mat[2], normal);
239         if (normalize_v3(mat[2]) == 0.0f) {
240                 return false;  /* error return */
241         }
242
243         cross_v3_v3v3(mat[0], mat[2], tangent);
244         if (is_zero_v3(mat[0])) {
245                 tangent[0] = 1.0f;
246                 tangent[1] = tangent[2] = 0.0f;
247                 cross_v3_v3v3(mat[0], tangent, mat[2]);
248         }
249
250         cross_v3_v3v3(mat[1], mat[2], mat[0]);
251
252         normalize_m3(mat);
253         
254         return true;
255 }
256
257 /**
258  * \note To recreate an orientation from the matrix:
259  * - (plane  == mat[1])
260  * - (normal == mat[2])
261  */
262 bool createSpaceNormalTangent(float mat[3][3], const float normal[3], const float tangent[3])
263 {
264         if (normalize_v3_v3(mat[2], normal) == 0.0f) {
265                 return false;  /* error return */
266         }
267
268         /* negate so we can use values from the matrix as input */
269         negate_v3_v3(mat[1], tangent);
270         /* preempt zero length tangent from causing trouble */
271         if (is_zero_v3(mat[1])) {
272                 mat[1][2] = 1.0f;
273         }
274
275         cross_v3_v3v3(mat[0], mat[2], mat[1]);
276         if (normalize_v3(mat[0]) == 0.0f) {
277                 return false;  /* error return */
278         }
279         
280         cross_v3_v3v3(mat[1], mat[2], mat[0]);
281         normalize_v3(mat[1]);
282
283         /* final matrix must be normalized, do inline */
284         // normalize_m3(mat);
285         
286         return true;
287 }
288
289 void BIF_createTransformOrientation(bContext *C, ReportList *reports,
290                                     const char *name, const bool use_view,
291                                     const bool activate, const bool overwrite)
292 {
293         TransformOrientation *ts = NULL;
294
295         if (use_view) {
296                 ts = createViewSpace(C, reports, name, overwrite);
297         }
298         else {
299                 Object *obedit = CTX_data_edit_object(C);
300                 Object *ob = CTX_data_active_object(C);
301                 if (obedit) {
302                         if (obedit->type == OB_MESH)
303                                 ts = createMeshSpace(C, reports, name, overwrite);
304                         else if (obedit->type == OB_ARMATURE)
305                                 ts = createBoneSpace(C, reports, name, overwrite);
306                         else if (obedit->type == OB_CURVE)
307                                 ts = createCurveSpace(C, reports, name, overwrite);
308                 }
309                 else if (ob && (ob->mode & OB_MODE_POSE)) {
310                         ts = createBoneSpace(C, reports, name, overwrite);
311                 }
312                 else {
313                         ts = createObjectSpace(C, reports, name, overwrite);
314                 }
315         }
316
317         if (activate && ts != NULL) {
318                 BIF_selectTransformOrientation(C, ts);
319         }
320 }
321
322 TransformOrientation *addMatrixSpace(bContext *C, float mat[3][3],
323                                      const char *name, const bool overwrite)
324 {
325         TransformOrientation *ts = NULL;
326         Scene *scene = CTX_data_scene(C);
327         ListBase *transform_orientations = &scene->transform_spaces;
328         char name_unique[sizeof(ts->name)];
329
330         if (overwrite) {
331                 ts = findOrientationName(transform_orientations, name);
332         }
333         else {
334                 BLI_strncpy(name_unique, name, sizeof(name_unique));
335                 uniqueOrientationName(transform_orientations, name_unique);
336                 name = name_unique;
337         }
338
339         /* if not, create a new one */
340         if (ts == NULL) {
341                 ts = MEM_callocN(sizeof(TransformOrientation), "UserTransSpace from matrix");
342                 BLI_addtail(transform_orientations, ts);
343                 BLI_strncpy(ts->name, name, sizeof(ts->name));
344         }
345
346         /* copy matrix into transform space */
347         copy_m3_m3(ts->mat, mat);
348
349         return ts;
350 }
351
352 void BIF_removeTransformOrientation(bContext *C, TransformOrientation *target)
353 {
354         BKE_scene_transform_orientation_remove(CTX_data_scene(C), target);
355 }
356
357 void BIF_removeTransformOrientationIndex(bContext *C, int index)
358 {
359         TransformOrientation *target = BKE_scene_transform_orientation_find(CTX_data_scene(C), index);
360         BIF_removeTransformOrientation(C, target);
361 }
362
363 void BIF_selectTransformOrientation(bContext *C, TransformOrientation *target)
364 {
365         Scene *scene = CTX_data_scene(C);
366         int index = BKE_scene_transform_orientation_get_index(scene, target);
367
368         BLI_assert(index != -1);
369
370         scene->orientation_type = V3D_MANIP_CUSTOM;
371         scene->orientation_index_custom = index;
372 }
373
374 /**
375  * Activate a transform orientation in a 3D view based on an enum value.
376  *
377  * \param orientation: If this is #V3D_MANIP_CUSTOM or greater, the custom transform orientation
378  *                     with index \a orientation - #V3D_MANIP_CUSTOM gets activated.
379  */
380 void BIF_selectTransformOrientationValue(Scene *scene, int orientation)
381 {
382         const bool is_custom = orientation >= V3D_MANIP_CUSTOM;
383         scene->orientation_type = is_custom ? V3D_MANIP_CUSTOM : orientation;
384         scene->orientation_index_custom = is_custom ? (orientation - V3D_MANIP_CUSTOM) : -1;
385 }
386
387 int BIF_countTransformOrientation(const bContext *C)
388 {
389         Scene *scene = CTX_data_scene(C);
390         ListBase *transform_orientations = &scene->transform_spaces;
391         return BLI_listbase_count(transform_orientations);
392 }
393
394 bool applyTransformOrientation(const TransformOrientation *ts, float r_mat[3][3], char *r_name)
395 {
396         if (r_name) {
397                 BLI_strncpy(r_name, ts->name, MAX_NAME);
398         }
399         copy_m3_m3(r_mat, ts->mat);
400
401         return true;
402 }
403
404 static int count_bone_select(bArmature *arm, ListBase *lb, const bool do_it)
405 {
406         Bone *bone;
407         bool do_next;
408         int total = 0;
409         
410         for (bone = lb->first; bone; bone = bone->next) {
411                 bone->flag &= ~BONE_TRANSFORM;
412                 do_next = do_it;
413                 if (do_it) {
414                         if (bone->layer & arm->layer) {
415                                 if (bone->flag & BONE_SELECTED) {
416                                         bone->flag |= BONE_TRANSFORM;
417                                         total++;
418
419                                         /* no transform on children if one parent bone is selected */
420                                         do_next = false;
421                                 }
422                         }
423                 }
424                 total += count_bone_select(arm, &bone->childbase, do_next);
425         }
426         
427         return total;
428 }
429
430 void initTransformOrientation(bContext *C, TransInfo *t)
431 {
432         Object *ob = CTX_data_active_object(C);
433         Object *obedit = CTX_data_active_object(C);
434
435         switch (t->current_orientation) {
436                 case V3D_MANIP_GLOBAL:
437                         unit_m3(t->spacemtx);
438                         BLI_strncpy(t->spacename, IFACE_("global"), sizeof(t->spacename));
439                         break;
440
441                 case V3D_MANIP_GIMBAL:
442                         unit_m3(t->spacemtx);
443                         if (ob && gimbal_axis(ob, t->spacemtx)) {
444                                 BLI_strncpy(t->spacename, IFACE_("gimbal"), sizeof(t->spacename));
445                                 break;
446                         }
447                         ATTR_FALLTHROUGH;  /* no gimbal fallthrough to normal */
448                 case V3D_MANIP_NORMAL:
449                         if (obedit || (ob && ob->mode & OB_MODE_POSE)) {
450                                 BLI_strncpy(t->spacename, IFACE_("normal"), sizeof(t->spacename));
451                                 ED_getTransformOrientationMatrix(C, t->spacemtx, t->around);
452                                 break;
453                         }
454                         ATTR_FALLTHROUGH;  /* we define 'normal' as 'local' in Object mode */
455                 case V3D_MANIP_LOCAL:
456                         BLI_strncpy(t->spacename, IFACE_("local"), sizeof(t->spacename));
457                 
458                         if (ob) {
459                                 copy_m3_m4(t->spacemtx, ob->obmat);
460                                 normalize_m3(t->spacemtx);
461                         }
462                         else {
463                                 unit_m3(t->spacemtx);
464                         }
465                 
466                         break;
467                 
468                 case V3D_MANIP_VIEW:
469                         if ((t->spacetype == SPACE_VIEW3D) &&
470                             (t->ar->regiontype == RGN_TYPE_WINDOW))
471                         {
472                                 RegionView3D *rv3d = t->ar->regiondata;
473                                 float mat[3][3];
474
475                                 BLI_strncpy(t->spacename, IFACE_("view"), sizeof(t->spacename));
476                                 copy_m3_m4(mat, rv3d->viewinv);
477                                 normalize_m3(mat);
478                                 copy_m3_m3(t->spacemtx, mat);
479                         }
480                         else {
481                                 unit_m3(t->spacemtx);
482                         }
483                         break;
484                 case V3D_MANIP_CUSTOM:
485                         BLI_strncpy(t->spacename, t->custom_orientation->name, sizeof(t->spacename));
486
487                         if (applyTransformOrientation(t->custom_orientation, t->spacemtx, t->spacename)) {
488                                 /* pass */
489                         }
490                         else {
491                                 unit_m3(t->spacemtx);
492                         }
493                         break;
494         }
495 }
496
497 /**
498  * utility function - get first n, selected vert/edge/faces
499  */
500 static unsigned int bm_mesh_elems_select_get_n__internal(
501         BMesh *bm, BMElem **elems, const unsigned int n,
502         const BMIterType itype, const char htype)
503 {
504         BMIter iter;
505         BMElem *ele;
506         unsigned int i;
507
508         BLI_assert(ELEM(htype, BM_VERT, BM_EDGE, BM_FACE));
509         BLI_assert(ELEM(itype, BM_VERTS_OF_MESH, BM_EDGES_OF_MESH, BM_FACES_OF_MESH));
510
511         if (!BLI_listbase_is_empty(&bm->selected)) {
512                 /* quick check */
513                 BMEditSelection *ese;
514                 i = 0;
515                 for (ese = bm->selected.last; ese; ese = ese->prev) {
516                         /* shouldn't need this check */
517                         if (BM_elem_flag_test(ese->ele, BM_ELEM_SELECT)) {
518
519                                 /* only use contiguous selection */
520                                 if (ese->htype != htype) {
521                                         i = 0;
522                                         break;
523                                 }
524
525                                 elems[i++] = ese->ele;
526                                 if (n == i) {
527                                         break;
528                                 }
529                         }
530                         else {
531                                 BLI_assert(0);
532                         }
533                 }
534
535                 if (i == 0) {
536                         /* pass */
537                 }
538                 else if (i == n) {
539                         return i;
540                 }
541         }
542
543         i = 0;
544         BM_ITER_MESH (ele, &iter, bm, itype) {
545                 BLI_assert(ele->head.htype == htype);
546                 if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
547                         elems[i++] = ele;
548                         if (n == i) {
549                                 break;
550                         }
551                 }
552         }
553
554         return i;
555 }
556
557 static unsigned int bm_mesh_verts_select_get_n(BMesh *bm, BMVert **elems, const unsigned int n)
558 {
559         return bm_mesh_elems_select_get_n__internal(
560                 bm, (BMElem **)elems, min_ii(n, bm->totvertsel),
561                 BM_VERTS_OF_MESH, BM_VERT);
562 }
563 static unsigned int bm_mesh_edges_select_get_n(BMesh *bm, BMEdge **elems, const unsigned int n)
564 {
565         return bm_mesh_elems_select_get_n__internal(
566                 bm, (BMElem **)elems, min_ii(n, bm->totedgesel),
567                 BM_EDGES_OF_MESH, BM_EDGE);
568 }
569 #if 0
570 static unsigned int bm_mesh_faces_select_get_n(BMesh *bm, BMVert **elems, const unsigned int n)
571 {
572         return bm_mesh_elems_select_get_n__internal(
573                 bm, (BMElem **)elems, min_ii(n, bm->totfacesel),
574                 BM_FACES_OF_MESH, BM_FACE);
575 }
576 #endif
577
578 int getTransformOrientation_ex(const bContext *C, float normal[3], float plane[3], const short around)
579 {
580         ViewLayer *view_layer = CTX_data_view_layer(C);
581         Object *obedit = CTX_data_edit_object(C);
582         Base *base;
583         Object *ob = OBACT(view_layer);
584         int result = ORIENTATION_NONE;
585         const bool activeOnly = (around == V3D_AROUND_ACTIVE);
586
587         zero_v3(normal);
588         zero_v3(plane);
589
590         if (obedit) {
591                 float imat[3][3], mat[3][3];
592                 
593                 /* we need the transpose of the inverse for a normal... */
594                 copy_m3_m4(imat, ob->obmat);
595                 
596                 invert_m3_m3(mat, imat);
597                 transpose_m3(mat);
598
599                 ob = obedit;
600
601                 if (ob->type == OB_MESH) {
602                         BMEditMesh *em = BKE_editmesh_from_object(ob);
603                         BMEditSelection ese;
604                         float vec[3] = {0, 0, 0};
605                         
606                         /* USE LAST SELECTED WITH ACTIVE */
607                         if (activeOnly && BM_select_history_active_get(em->bm, &ese)) {
608                                 BM_editselection_normal(&ese, normal);
609                                 BM_editselection_plane(&ese, plane);
610                                 
611                                 switch (ese.htype) {
612                                         case BM_VERT:
613                                                 result = ORIENTATION_VERT;
614                                                 break;
615                                         case BM_EDGE:
616                                                 result = ORIENTATION_EDGE;
617                                                 break;
618                                         case BM_FACE:
619                                                 result = ORIENTATION_FACE;
620                                                 break;
621                                 }
622                         }
623                         else {
624                                 if (em->bm->totfacesel >= 1) {
625                                         BMFace *efa;
626                                         BMIter iter;
627
628                                         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
629                                                 if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
630                                                         BM_face_calc_tangent_auto(efa, vec);
631                                                         add_v3_v3(normal, efa->no);
632                                                         add_v3_v3(plane, vec);
633                                                 }
634                                         }
635                                         
636                                         result = ORIENTATION_FACE;
637                                 }
638                                 else if (em->bm->totvertsel == 3) {
639                                         BMVert *v_tri[3];
640
641                                         if (bm_mesh_verts_select_get_n(em->bm, v_tri, 3) == 3) {
642                                                 BMEdge *e = NULL;
643                                                 float no_test[3];
644
645                                                 normal_tri_v3(normal, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co);
646
647                                                 /* check if the normal is pointing opposite to vert normals */
648                                                 no_test[0] = v_tri[0]->no[0] + v_tri[1]->no[0] + v_tri[2]->no[0];
649                                                 no_test[1] = v_tri[0]->no[1] + v_tri[1]->no[1] + v_tri[2]->no[1];
650                                                 no_test[2] = v_tri[0]->no[2] + v_tri[1]->no[2] + v_tri[2]->no[2];
651                                                 if (dot_v3v3(no_test, normal) < 0.0f) {
652                                                         negate_v3(normal);
653                                                 }
654
655                                                 if (em->bm->totedgesel >= 1) {
656                                                         /* find an edge thats apart of v_tri (no need to search all edges) */
657                                                         float e_length;
658                                                         int j;
659
660                                                         for (j = 0; j < 3; j++) {
661                                                                 BMEdge *e_test = BM_edge_exists(v_tri[j], v_tri[(j + 1) % 3]);
662                                                                 if (e_test && BM_elem_flag_test(e_test, BM_ELEM_SELECT)) {
663                                                                         const float e_test_length = BM_edge_calc_length_squared(e_test);
664                                                                         if ((e == NULL) || (e_length < e_test_length)) {
665                                                                                 e = e_test;
666                                                                                 e_length = e_test_length;
667                                                                         }
668                                                                 }
669                                                         }
670                                                 }
671
672                                                 if (e) {
673                                                         BMVert *v_pair[2];
674                                                         if (BM_edge_is_boundary(e)) {
675                                                                 BM_edge_ordered_verts(e, &v_pair[0], &v_pair[1]);
676                                                         }
677                                                         else {
678                                                                 v_pair[0] = e->v1;
679                                                                 v_pair[1] = e->v2;
680                                                         }
681                                                         sub_v3_v3v3(plane, v_pair[0]->co, v_pair[1]->co);
682                                                 }
683                                                 else {
684                                                         BM_vert_tri_calc_tangent_edge(v_tri, plane);
685                                                 }
686                                         }
687                                         else {
688                                                 BLI_assert(0);
689                                         }
690
691                                         result = ORIENTATION_FACE;
692                                 }
693                                 else if (em->bm->totedgesel == 1 || em->bm->totvertsel == 2) {
694                                         BMVert *v_pair[2] = {NULL, NULL};
695                                         BMEdge *eed = NULL;
696                                         
697                                         if (em->bm->totedgesel == 1) {
698                                                 if (bm_mesh_edges_select_get_n(em->bm, &eed, 1) == 1) {
699                                                         v_pair[0] = eed->v1;
700                                                         v_pair[1] = eed->v2;
701                                                 }
702                                         }
703                                         else {
704                                                 BLI_assert(em->bm->totvertsel == 2);
705                                                 bm_mesh_verts_select_get_n(em->bm, v_pair, 2);
706                                         }
707
708                                         /* should never fail */
709                                         if (LIKELY(v_pair[0] && v_pair[1])) {
710                                                 bool v_pair_swap = false;
711                                                 /**
712                                                  * Logic explained:
713                                                  *
714                                                  * - Edges and vert-pairs treated the same way.
715                                                  * - Point the Y axis along the edge vector (towards the active vertex).
716                                                  * - Point the Z axis outwards (the same direction as the normals).
717                                                  *
718                                                  * \note Z points outwards - along the normal.
719                                                  * take care making changes here, see: T38592, T43708
720                                                  */
721
722                                                 /* be deterministic where possible and ensure v_pair[0] is active */
723                                                 if (BM_mesh_active_vert_get(em->bm) == v_pair[1]) {
724                                                         v_pair_swap = true;
725                                                 }
726                                                 else if (eed && BM_edge_is_boundary(eed)) {
727                                                         /* predictable direction for boundary edges */
728                                                         if (eed->l->v != v_pair[0]) {
729                                                                 v_pair_swap = true;
730                                                         }
731                                                 }
732
733                                                 if (v_pair_swap) {
734                                                         SWAP(BMVert *, v_pair[0], v_pair[1]);
735                                                 }
736
737                                                 add_v3_v3v3(normal, v_pair[1]->no, v_pair[0]->no);
738                                                 sub_v3_v3v3(plane, v_pair[1]->co, v_pair[0]->co);
739
740                                                 if (normalize_v3(plane) != 0.0f) {
741                                                         /* For edges it'd important the resulting matrix can rotate around the edge,
742                                                          * project onto the plane so we can use a fallback value. */
743                                                         project_plane_normalized_v3_v3v3(normal, normal, plane);
744                                                         if (UNLIKELY(normalize_v3(normal) == 0.0f)) {
745                                                                 /* in the case the normal and plane are aligned,
746                                                                  * use a fallback normal which is orthogonal to the plane. */
747                                                                 ortho_v3_v3(normal, plane);
748                                                         }
749                                                 }
750                                         }
751
752                                         result = ORIENTATION_EDGE;
753                                 }
754                                 else if (em->bm->totvertsel == 1) {
755                                         BMVert *v = NULL;
756
757                                         if (bm_mesh_verts_select_get_n(em->bm, &v, 1) == 1) {
758                                                 copy_v3_v3(normal, v->no);
759                                                 BMEdge *e_pair[2];
760
761                                                 if (BM_vert_edge_pair(v, &e_pair[0], &e_pair[1])) {
762                                                         bool v_pair_swap = false;
763                                                         BMVert *v_pair[2] = {BM_edge_other_vert(e_pair[0], v), BM_edge_other_vert(e_pair[1], v)};
764                                                         float dir_pair[2][3];
765
766                                                         if (BM_edge_is_boundary(e_pair[0])) {
767                                                                 if (e_pair[0]->l->v != v) {
768                                                                         v_pair_swap = true;
769                                                                 }
770                                                         }
771                                                         else {
772                                                                 if (BM_edge_calc_length_squared(e_pair[0]) < BM_edge_calc_length_squared(e_pair[1])) {
773                                                                         v_pair_swap = true;
774                                                                 }
775                                                         }
776
777                                                         if (v_pair_swap) {
778                                                                 SWAP(BMVert *, v_pair[0], v_pair[1]);
779                                                         }
780
781                                                         sub_v3_v3v3(dir_pair[0], v->co, v_pair[0]->co);
782                                                         sub_v3_v3v3(dir_pair[1], v_pair[1]->co, v->co);
783                                                         normalize_v3(dir_pair[0]);
784                                                         normalize_v3(dir_pair[1]);
785
786                                                         add_v3_v3v3(plane, dir_pair[0], dir_pair[1]);
787                                                 }
788                                         }
789
790                                         if (is_zero_v3(plane)) {
791                                                 result = ORIENTATION_VERT;
792                                         }
793                                         else {
794                                                 result = ORIENTATION_EDGE;
795                                         }
796                                 }
797                                 else if (em->bm->totvertsel > 3) {
798                                         BMIter iter;
799                                         BMVert *v;
800
801                                         zero_v3(normal);
802
803                                         BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
804                                                 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
805                                                         add_v3_v3(normal, v->no);
806                                                 }
807                                         }
808                                         normalize_v3(normal);
809                                         result = ORIENTATION_VERT;
810                                 }
811                         }
812
813                         /* not needed but this matches 2.68 and older behavior */
814                         negate_v3(plane);
815
816                 } /* end editmesh */
817                 else if (ELEM(obedit->type, OB_CURVE, OB_SURF)) {
818                         Curve *cu = obedit->data;
819                         Nurb *nu = NULL;
820                         int a;
821                         ListBase *nurbs = BKE_curve_editNurbs_get(cu);
822
823                         void *vert_act = NULL;
824                         if (activeOnly && BKE_curve_nurb_vert_active_get(cu, &nu, &vert_act)) {
825                                 if (nu->type == CU_BEZIER) {
826                                         BezTriple *bezt = vert_act;
827                                         BKE_nurb_bezt_calc_normal(nu, bezt, normal);
828                                         BKE_nurb_bezt_calc_plane(nu, bezt, plane);
829                                 }
830                                 else {
831                                         BPoint *bp = vert_act;
832                                         BKE_nurb_bpoint_calc_normal(nu, bp, normal);
833                                         BKE_nurb_bpoint_calc_plane(nu, bp, plane);
834                                 }
835                         }
836                         else {
837                                 const bool use_handle = (cu->drawflag & CU_HIDE_HANDLES) == 0;
838
839                                 for (nu = nurbs->first; nu; nu = nu->next) {
840                                         /* only bezier has a normal */
841                                         if (nu->type == CU_BEZIER) {
842                                                 BezTriple *bezt = nu->bezt;
843                                                 a = nu->pntsu;
844                                                 while (a--) {
845                                                         short flag = 0;
846
847 #define SEL_F1 (1 << 0)
848 #define SEL_F2 (1 << 1)
849 #define SEL_F3 (1 << 2)
850
851                                                         if (use_handle) {
852                                                                 if (bezt->f1 & SELECT) flag |= SEL_F1;
853                                                                 if (bezt->f2 & SELECT) flag |= SEL_F2;
854                                                                 if (bezt->f3 & SELECT) flag |= SEL_F3;
855                                                         }
856                                                         else {
857                                                                 flag = (bezt->f2 & SELECT) ? (SEL_F1 | SEL_F2 | SEL_F3) : 0;
858                                                         }
859
860                                                         /* exception */
861                                                         if (flag) {
862                                                                 float tvec[3];
863                                                                 if ((around == V3D_AROUND_LOCAL_ORIGINS) ||
864                                                                     ELEM(flag, SEL_F2, SEL_F1 | SEL_F3, SEL_F1 | SEL_F2 | SEL_F3))
865                                                                 {
866                                                                         BKE_nurb_bezt_calc_normal(nu, bezt, tvec);
867                                                                         add_v3_v3(normal, tvec);
868                                                                 }
869                                                                 else {
870                                                                         /* ignore bezt->f2 in this case */
871                                                                         if (flag & SEL_F1) {
872                                                                                 sub_v3_v3v3(tvec, bezt->vec[0], bezt->vec[1]);
873                                                                                 normalize_v3(tvec);
874                                                                                 add_v3_v3(normal, tvec);
875                                                                         }
876                                                                         if (flag & SEL_F3) {
877                                                                                 sub_v3_v3v3(tvec, bezt->vec[1], bezt->vec[2]);
878                                                                                 normalize_v3(tvec);
879                                                                                 add_v3_v3(normal, tvec);
880                                                                         }
881                                                                 }
882
883                                                                 BKE_nurb_bezt_calc_plane(nu, bezt, tvec);
884                                                                 add_v3_v3(plane, tvec);
885                                                         }
886
887 #undef SEL_F1
888 #undef SEL_F2
889 #undef SEL_F3
890
891                                                         bezt++;
892                                                 }
893                                         }
894                                         else if (nu->bp && (nu->pntsv == 1)) {
895                                                 BPoint *bp = nu->bp;
896                                                 a = nu->pntsu;
897                                                 while (a--) {
898                                                         if (bp->f1 & SELECT) {
899                                                                 float tvec[3];
900
901                                                                 BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp);
902                                                                 BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp);
903
904                                                                 const bool is_prev_sel = bp_prev && (bp_prev->f1 & SELECT);
905                                                                 const bool is_next_sel = bp_next && (bp_next->f1 & SELECT);
906                                                                 if (is_prev_sel == false && is_next_sel == false) {
907                                                                         /* Isolated, add based on surrounding */
908                                                                         BKE_nurb_bpoint_calc_normal(nu, bp, tvec);
909                                                                         add_v3_v3(normal, tvec);
910                                                                 }
911                                                                 else if (is_next_sel) {
912                                                                         /* A segment, add the edge normal */
913                                                                         sub_v3_v3v3(tvec, bp->vec, bp_next->vec);
914                                                                         normalize_v3(tvec);
915                                                                         add_v3_v3(normal, tvec);
916                                                                 }
917
918                                                                 BKE_nurb_bpoint_calc_plane(nu, bp, tvec);
919                                                                 add_v3_v3(plane, tvec);
920                                                         }
921                                                         bp++;
922                                                 }
923                                         }
924                                 }
925                         }
926                         
927                         if (!is_zero_v3(normal)) {
928                                 result = ORIENTATION_FACE;
929                         }
930                 }
931                 else if (obedit->type == OB_MBALL) {
932                         MetaBall *mb = obedit->data;
933                         MetaElem *ml;
934                         bool ok = false;
935                         float tmat[3][3];
936                         
937                         if (activeOnly && (ml = mb->lastelem)) {
938                                 quat_to_mat3(tmat, ml->quat);
939                                 add_v3_v3(normal, tmat[2]);
940                                 add_v3_v3(plane, tmat[1]);
941                                 ok = true;
942                         }
943                         else {
944                                 for (ml = mb->editelems->first; ml; ml = ml->next) {
945                                         if (ml->flag & SELECT) {
946                                                 quat_to_mat3(tmat, ml->quat);
947                                                 add_v3_v3(normal, tmat[2]);
948                                                 add_v3_v3(plane, tmat[1]);
949                                                 ok = true;
950                                         }
951                                 }
952                         }
953
954                         if (ok) {
955                                 if (!is_zero_v3(plane)) {
956                                         result = ORIENTATION_FACE;
957                                 }
958                         }
959                 }
960                 else if (obedit->type == OB_ARMATURE) {
961                         bArmature *arm = obedit->data;
962                         EditBone *ebone;
963                         bool ok = false;
964                         float tmat[3][3];
965
966                         if (activeOnly && (ebone = arm->act_edbone)) {
967                                 ED_armature_ebone_to_mat3(ebone, tmat);
968                                 add_v3_v3(normal, tmat[2]);
969                                 add_v3_v3(plane, tmat[1]);
970                                 ok = true;
971                         }
972                         else {
973                                 for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
974                                         if (arm->layer & ebone->layer) {
975                                                 if (ebone->flag & BONE_SELECTED) {
976                                                         ED_armature_ebone_to_mat3(ebone, tmat);
977                                                         add_v3_v3(normal, tmat[2]);
978                                                         add_v3_v3(plane, tmat[1]);
979                                                         ok = true;
980                                                 }
981                                         }
982                                 }
983                         }
984                         
985                         if (ok) {
986                                 if (!is_zero_v3(plane)) {
987                                         result = ORIENTATION_EDGE;
988                                 }
989                         }
990                 }
991
992                 /* Vectors from edges don't need the special transpose inverse multiplication */
993                 if (result == ORIENTATION_EDGE) {
994                         float tvec[3];
995
996                         mul_mat3_m4_v3(ob->obmat, normal);
997                         mul_mat3_m4_v3(ob->obmat, plane);
998
999                         /* align normal to edge direction (so normal is perpendicular to the plane).
1000                          * 'ORIENTATION_EDGE' will do the other way around.
1001                          * This has to be done **after** applying obmat, see T45775! */
1002                         project_v3_v3v3(tvec, normal, plane);
1003                         sub_v3_v3(normal, tvec);
1004                 }
1005                 else {
1006                         mul_m3_v3(mat, normal);
1007                         mul_m3_v3(mat, plane);
1008                 }
1009         }
1010         else if (ob && (ob->mode & OB_MODE_POSE)) {
1011                 bArmature *arm = ob->data;
1012                 bPoseChannel *pchan;
1013                 float imat[3][3], mat[3][3];
1014                 bool ok = false;
1015
1016                 if (activeOnly && (pchan = BKE_pose_channel_active(ob))) {
1017                         add_v3_v3(normal, pchan->pose_mat[2]);
1018                         add_v3_v3(plane, pchan->pose_mat[1]);
1019                         ok = true;
1020                 }
1021                 else {
1022                         int totsel;
1023
1024                         totsel = count_bone_select(arm, &arm->bonebase, true);
1025                         if (totsel) {
1026                                 /* use channels to get stats */
1027                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1028                                         if (pchan->bone && pchan->bone->flag & BONE_TRANSFORM) {
1029                                                 add_v3_v3(normal, pchan->pose_mat[2]);
1030                                                 add_v3_v3(plane, pchan->pose_mat[1]);
1031                                         }
1032                                 }
1033                                 ok = true;
1034                         }
1035                 }
1036
1037                 /* use for both active & all */
1038                 if (ok) {
1039                         /* we need the transpose of the inverse for a normal... */
1040                         copy_m3_m4(imat, ob->obmat);
1041                         
1042                         invert_m3_m3(mat, imat);
1043                         transpose_m3(mat);
1044                         mul_m3_v3(mat, normal);
1045                         mul_m3_v3(mat, plane);
1046                         
1047                         result = ORIENTATION_EDGE;
1048                 }
1049         }
1050         else if (ob && (ob->mode & (OB_MODE_ALL_PAINT | OB_MODE_PARTICLE_EDIT))) {
1051                 /* pass */
1052         }
1053         else {
1054                 /* we need the one selected object, if its not active */
1055                 base = BASACT(view_layer);
1056                 ob = OBACT(view_layer);
1057                 if (base && ((base->flag & BASE_SELECTED) != 0)) {
1058                         /* pass */
1059                 }
1060                 else {
1061                         /* first selected */
1062                         ob = NULL;
1063                         for (base = view_layer->object_bases.first; base; base = base->next) {
1064                                 if (TESTBASELIB(base)) {
1065                                         ob = base->object;
1066                                         break;
1067                                 }
1068                         }
1069                 }
1070                 
1071                 if (ob) {
1072                         copy_v3_v3(normal, ob->obmat[2]);
1073                         copy_v3_v3(plane, ob->obmat[1]);
1074                 }
1075                 result = ORIENTATION_NORMAL;
1076         }
1077         
1078         return result;
1079 }
1080
1081 int getTransformOrientation(const bContext *C, float normal[3], float plane[3])
1082 {
1083         /* dummy value, not V3D_AROUND_ACTIVE and not V3D_AROUND_LOCAL_ORIGINS */
1084         short around = V3D_AROUND_CENTER_BOUNDS;
1085
1086         return getTransformOrientation_ex(C, normal, plane, around);
1087 }
1088
1089 void ED_getTransformOrientationMatrix(const bContext *C, float orientation_mat[3][3], const short around)
1090 {
1091         float normal[3] = {0.0, 0.0, 0.0};
1092         float plane[3] = {0.0, 0.0, 0.0};
1093
1094         int type;
1095
1096         type = getTransformOrientation_ex(C, normal, plane, around);
1097
1098         switch (type) {
1099                 case ORIENTATION_NORMAL:
1100                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0) {
1101                                 type = ORIENTATION_NONE;
1102                         }
1103                         break;
1104                 case ORIENTATION_VERT:
1105                         if (createSpaceNormal(orientation_mat, normal) == 0) {
1106                                 type = ORIENTATION_NONE;
1107                         }
1108                         break;
1109                 case ORIENTATION_EDGE:
1110                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0) {
1111                                 type = ORIENTATION_NONE;
1112                         }
1113                         break;
1114                 case ORIENTATION_FACE:
1115                         if (createSpaceNormalTangent(orientation_mat, normal, plane) == 0) {
1116                                 type = ORIENTATION_NONE;
1117                         }
1118                         break;
1119         }
1120
1121         if (type == ORIENTATION_NONE) {
1122                 unit_m3(orientation_mat);
1123         }
1124 }