Blender V5.3
transform_orientations.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cctype>
10#include <cstddef>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "DNA_armature_types.h"
16#include "DNA_curve_types.h"
17#include "DNA_meta_types.h"
18#include "DNA_object_types.h"
19#include "DNA_scene_types.h"
20#include "DNA_screen_types.h"
21#include "DNA_sequence_types.h"
22#include "DNA_space_types.h"
23#include "DNA_view3d_types.h"
24
25#include "BLI_listbase.hh"
26#include "BLI_math_geom_c.hh"
27#include "BLI_math_matrix_c.hh"
29#include "BLI_math_vector_c.hh"
30#include "BLI_string_utf8.hh"
31#include "BLI_string_utils.hh"
32#include "BLI_utildefines.hh"
33
34#include "BKE_action.hh"
35#include "BKE_armature.hh"
36#include "BKE_context.hh"
37#include "BKE_curve.hh"
38#include "BKE_editmesh.hh"
39#include "BKE_layer.hh"
40#include "BKE_report.hh"
41#include "BKE_scene.hh"
42
43#include "BLT_translation.hh"
44
45#include "ED_armature.hh"
46
47#include "ANIM_armature.hh"
49
50#include "SEQ_select.hh"
51
52#include "SEQ_transform.hh"
53#include "transform.hh"
55
56namespace blender::ed::transform {
57
58/* *********************** TransSpace ************************** */
59
61{
62 Scene *scene = CTX_data_scene(C);
63 ListBaseT<TransformOrientation> *transform_orientations = &scene->transform_spaces;
64
65 transform_orientations->free_no_destruct();
66
67 for (int i = 0; i < ARRAY_SIZE(scene->orientation_slots); i++) {
68 TransformOrientationSlot *orient_slot = &scene->orientation_slots[i];
69 if (orient_slot->type == V3D_ORIENT_CUSTOM) {
70 orient_slot->type = V3D_ORIENT_GLOBAL; /* Fallback to global. */
71 orient_slot->index_custom = -1;
72 }
73 }
74}
75
76static TransformOrientation *findOrientationName(ListBaseT<TransformOrientation> *lb,
77 const char *name)
78{
79 return static_cast<TransformOrientation *>(
81}
82
83static void uniqueOrientationName(ListBaseT<TransformOrientation> *lb, char *name)
84{
86 [&](const StringRefNull check_name) {
87 return findOrientationName(lb, check_name.c_str()) != nullptr;
88 },
90 '.',
91 name,
93}
94
96 ReportList * /*reports*/,
97 const char *name,
98 const bool overwrite)
99{
101 float mat[3][3];
102
103 if (!rv3d) {
104 return nullptr;
105 }
106
107 copy_m3_m4(mat, rv3d->viewinv);
108 normalize_m3(mat);
109
110 if (name[0] == 0) {
111 View3D *v3d = CTX_wm_view3d(C);
112 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
113 /* If an object is used as camera, then this space is the same as object space! */
114 name = v3d->camera->id.name + 2;
115 }
116 else {
117 name = DATA_("Custom View");
118 }
119 }
120
121 return addMatrixSpace(C, mat, name, overwrite);
122}
123
125 ReportList * /*reports*/,
126 const char *name,
127 const bool overwrite)
128{
129 Base *base = CTX_data_active_base(C);
130 Object *ob;
131 float mat[3][3];
132
133 if (base == nullptr) {
134 return nullptr;
135 }
136
137 ob = base->object;
138
139 copy_m3_m4(mat, ob->object_to_world().ptr());
140 normalize_m3(mat);
141
142 /* Use object name if no name is given. */
143 if (name[0] == 0) {
144 name = ob->id.name + 2;
145 }
146
147 return addMatrixSpace(C, mat, name, overwrite);
148}
149
151 ReportList *reports,
152 const char *name,
153 const bool overwrite)
154{
155 float mat[3][3];
156 float normal[3], plane[3];
157
158 getTransformOrientation(C, normal, plane);
159
160 if (createSpaceNormalTangent(mat, normal, plane) == 0) {
161 BKE_reports_prepend(reports, "Cannot use zero-length bone");
162 return nullptr;
163 }
164
165 if (name[0] == 0) {
167 }
168
169 return addMatrixSpace(C, mat, name, overwrite);
170}
171
173 ReportList *reports,
174 const char *name,
175 const bool overwrite)
176{
177 float mat[3][3];
178 float normal[3], plane[3];
179
180 getTransformOrientation(C, normal, plane);
181
182 if (createSpaceNormalTangent(mat, normal, plane) == 0) {
183 BKE_reports_prepend(reports, "Cannot use zero-length curve");
184 return nullptr;
185 }
186
187 if (name[0] == 0) {
188 name = DATA_("Curve");
189 }
190
191 return addMatrixSpace(C, mat, name, overwrite);
192}
193
195 ReportList *reports,
196 const char *name,
197 const bool overwrite)
198{
199 float mat[3][3];
200 float normal[3], plane[3];
201 int type;
202
203 type = getTransformOrientation(C, normal, plane);
204
205 switch (type) {
206 case ORIENTATION_VERT:
207 if (createSpaceNormal(mat, normal) == 0) {
208 BKE_reports_prepend(reports, "Cannot use vertex with zero-length normal");
209 return nullptr;
210 }
211
212 if (name[0] == 0) {
213 name = DATA_("Vertex");
214 }
215 break;
216 case ORIENTATION_EDGE:
217 if (createSpaceNormalTangent(mat, normal, plane) == 0) {
218 BKE_reports_prepend(reports, "Cannot use zero-length edge");
219 return nullptr;
220 }
221
222 if (name[0] == 0) {
223 name = DATA_("Edge");
224 }
225 break;
226 case ORIENTATION_FACE:
227 if (createSpaceNormalTangent(mat, normal, plane) == 0) {
228 BKE_reports_prepend(reports, "Cannot use zero-area face");
229 return nullptr;
230 }
231
232 if (name[0] == 0) {
233 name = DATA_("Face");
234 }
235 break;
236 default:
237 return nullptr;
238 }
239
240 return addMatrixSpace(C, mat, name, overwrite);
241}
242
243static bool test_rotmode_euler(short rotmode)
244{
245 return ELEM(rotmode, ROT_MODE_AXISANGLE, ROT_MODE_QUAT) ? false : true;
246}
247
252static void axis_angle_to_gimbal_axis(float gmat[3][3], const float axis[3], const float angle)
253{
254 /* X/Y are arbitrary axes, most importantly Z is the axis of rotation. */
255
256 float cross_vec[3];
257 float quat[4];
258
259 /* This is an un-scientific method to get a vector to cross with XYZ intentionally YZX. */
260 cross_vec[0] = axis[1];
261 cross_vec[1] = axis[2];
262 cross_vec[2] = axis[0];
263
264 /* X-axis. */
265 cross_v3_v3v3(gmat[0], cross_vec, axis);
266 normalize_v3(gmat[0]);
267 axis_angle_to_quat(quat, axis, angle);
268 mul_qt_v3(quat, gmat[0]);
269
270 /* Y-axis. */
271 axis_angle_to_quat(quat, axis, M_PI_2);
272 copy_v3_v3(gmat[1], gmat[0]);
273 mul_qt_v3(quat, gmat[1]);
274
275 /* Z-axis. */
276 copy_v3_v3(gmat[2], axis);
277
278 normalize_m3(gmat);
279}
280
281bool gimbal_axis_pose(Object *ob, const bPoseChannel *pchan, float gmat[3][3])
282{
283 float mat[3][3], tmat[3][3], obmat[3][3];
284 if (test_rotmode_euler(pchan->rotmode)) {
285 eulO_to_gimbal_axis(mat, pchan->eul, pchan->rotmode);
286 }
287 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
288 axis_angle_to_gimbal_axis(mat, pchan->rotAxis, pchan->rotAngle);
289 }
290 else { /* Quaternion. */
291 return false;
292 }
293
294 /* Apply bone transformation. */
295 const Bone *pchan_bone = pchan->bone_get(*ob);
296 mul_m3_m3m3(tmat, pchan_bone->bone_mat, mat);
297
298 if (pchan->parent) {
299 float parent_mat[3][3];
300
301 copy_m3_m4(parent_mat,
302 (pchan_bone->flag & BONE_HINGE) ? pchan_bone->parent->arm_mat :
303 pchan->parent->pose_mat);
304 mul_m3_m3m3(mat, parent_mat, tmat);
305
306 /* Needed if object transformation isn't identity. */
307 copy_m3_m4(obmat, ob->object_to_world().ptr());
308 mul_m3_m3m3(gmat, obmat, mat);
309 }
310 else {
311 /* Needed if object transformation isn't identity. */
312 copy_m3_m4(obmat, ob->object_to_world().ptr());
313 mul_m3_m3m3(gmat, obmat, tmat);
314 }
315
316 normalize_m3(gmat);
317 return true;
318}
319
320bool gimbal_axis_object(Object *ob, float gmat[3][3])
321{
322 if (test_rotmode_euler(ob->rotmode)) {
323 eulO_to_gimbal_axis(gmat, ob->rot, ob->rotmode);
324 }
325 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
327 }
328 else { /* Quaternion. */
329 return false;
330 }
331
332 if (ob->parent) {
333 float parent_mat[3][3];
334 copy_m3_m4(parent_mat, ob->parent->object_to_world().ptr());
335 normalize_m3(parent_mat);
336 mul_m3_m3m3(gmat, parent_mat, gmat);
337 }
338 return true;
339}
340
342 const float x[3],
343 const float y[3],
344 const float z[3])
345{
346 bool is_zero[3] = {true, true, true};
347 zero_m3(mat);
348 if (x) {
349 is_zero[0] = normalize_v3_v3(mat[0], x) == 0.0f;
350 }
351 if (y) {
352 is_zero[1] = normalize_v3_v3(mat[1], y) == 0.0f;
353 }
354 if (z) {
355 is_zero[2] = normalize_v3_v3(mat[2], z) == 0.0f;
356 }
357
358 int zero_axis = is_zero[0] + is_zero[1] + is_zero[2];
359 if (zero_axis == 0) {
360 return true;
361 }
362
363 if (zero_axis == 1) {
364 int axis = is_zero[0] ? 0 : is_zero[1] ? 1 : 2;
365 cross_v3_v3v3(mat[axis], mat[(axis + 1) % 3], mat[(axis + 2) % 3]);
366 if (normalize_v3(mat[axis]) != 0.0f) {
367 return true;
368 }
369 }
370 else if (zero_axis == 2) {
371 int axis, a, b;
372 axis = !is_zero[0] ? 0 : !is_zero[1] ? 1 : 2;
373 a = (axis + 1) % 3;
374 b = (axis + 2) % 3;
375
376 mat[a][a] = 1.0f;
377 mat[b][b] = 1.0f;
378 project_plane_v3_v3v3(mat[a], mat[a], mat[axis]);
379 project_plane_v3_v3v3(mat[b], mat[b], mat[axis]);
380 if ((normalize_v3(mat[a]) != 0.0f) && (normalize_v3(mat[b]) != 0.0f)) {
381 return true;
382 }
383 }
384
385 unit_m3(mat);
386 return false;
387}
388
389bool createSpaceNormal(float mat[3][3], const float normal[3])
390{
391 float tangent[3] = {0.0f, 0.0f, 1.0f};
392
393 copy_v3_v3(mat[2], normal);
394 if (normalize_v3(mat[2]) == 0.0f) {
395 return false; /* Error return. */
396 }
397
398 cross_v3_v3v3(mat[0], mat[2], tangent);
399 if (is_zero_v3(mat[0])) {
400 tangent[0] = 1.0f;
401 tangent[1] = tangent[2] = 0.0f;
402 cross_v3_v3v3(mat[0], tangent, mat[2]);
403 }
404
405 cross_v3_v3v3(mat[1], mat[2], mat[0]);
406
407 normalize_m3(mat);
408
409 return true;
410}
411
412bool createSpaceNormalTangent(float mat[3][3], const float normal[3], const float tangent[3])
413{
414 BLI_ASSERT_UNIT_V3(normal);
415 BLI_ASSERT_UNIT_V3(tangent);
416
417 if (is_zero_v3(normal)) [[unlikely]] {
418 /* Error return. */
419 return false;
420 }
421 copy_v3_v3(mat[2], normal);
422
423 /* Negate so we can use values from the matrix as input. */
424 negate_v3_v3(mat[1], tangent);
425
426 /* Preempt zero length tangent from causing trouble. */
427 if (is_zero_v3(mat[1])) [[unlikely]] {
428 mat[1][2] = 1.0f;
429 }
430
431 cross_v3_v3v3(mat[0], mat[2], mat[1]);
432 if (normalize_v3(mat[0]) == 0.0f) [[unlikely]] {
433 /* Error return from co-linear normal & tangent. */
434 return false;
435 }
436
437 /* Make the tangent orthogonal. */
438 cross_v3_v3v3(mat[1], mat[2], mat[0]);
439
440 if (normalize_v3(mat[1]) == 0.0f) [[unlikely]] {
441 /* Error return as it's possible making the tangent orthogonal to the normal
442 * causes it to be zero length. */
443 return false;
444 }
445
446 /* Final matrix must be normalized, do inline. */
447 // normalize_m3(mat);
448
449 return true;
450}
451
453 const float normal[3],
454 const float tangent[3])
455{
456 if (createSpaceNormalTangent(mat, normal, tangent)) {
457 return;
458 }
459 if (!is_zero_v3(normal)) {
460 axis_dominant_v3_to_m3(mat, normal);
461 invert_m3(mat);
462 return;
463 }
464 /* Last resort. */
465 unit_m3(mat);
466}
467
469 ReportList *reports,
470 const char *name,
471 const bool use_view,
472 const bool activate,
473 const bool overwrite)
474{
475 TransformOrientation *ts = nullptr;
476
477 if (use_view) {
478 ts = createViewSpace(C, reports, name, overwrite);
479 }
480 else {
481 Object *obedit = CTX_data_edit_object(C);
483 if (obedit) {
484 if (obedit->type == OB_MESH) {
485 ts = createMeshSpace(C, reports, name, overwrite);
486 }
487 else if (obedit->type == OB_ARMATURE) {
488 ts = createBoneSpace(C, reports, name, overwrite);
489 }
490 else if (obedit->type == OB_CURVES_LEGACY) {
491 ts = createCurveSpace(C, reports, name, overwrite);
492 }
493 }
494 else if (ob && (ob->mode & OB_MODE_POSE)) {
495 ts = createBoneSpace(C, reports, name, overwrite);
496 }
497 else {
498 ts = createObjectSpace(C, reports, name, overwrite);
499 }
500 }
501
502 if (activate && ts != nullptr) {
504 }
505 return (ts != nullptr);
506}
507
509 float mat[3][3],
510 const char *name,
511 const bool overwrite)
512{
513 TransformOrientation *ts = nullptr;
514 Scene *scene = CTX_data_scene(C);
515 ListBaseT<TransformOrientation> *transform_orientations = &scene->transform_spaces;
516 char name_unique[sizeof(ts->name)];
517
518 if (overwrite) {
519 ts = findOrientationName(transform_orientations, name);
520 }
521 else {
522 STRNCPY_UTF8(name_unique, name);
523 uniqueOrientationName(transform_orientations, name_unique);
524 name = name_unique;
525 }
526
527 /* If not, create a new one. */
528 if (ts == nullptr) {
529 ts = MEM_new<TransformOrientation>("UserTransSpace from matrix");
530 BLI_addtail(transform_orientations, ts);
531 STRNCPY_UTF8(ts->name, name);
532 }
533
534 /* Copy matrix into transform space. */
535 copy_m3_m3(ts->mat, mat);
536
537 return ts;
538}
539
544
550
561
563{
564 Scene *scene = CTX_data_scene(C);
565 ListBaseT<TransformOrientation> *transform_orientations = &scene->transform_spaces;
566 return transform_orientations->count();
567}
568
569void applyTransformOrientation(const TransformOrientation *ts, float r_mat[3][3], char r_name[64])
570{
571 if (r_name) {
572 BLI_strncpy_utf8(r_name, ts->name, MAX_NAME);
573 }
574 copy_m3_m3(r_mat, ts->mat);
575}
576
577static int bone_children_clear_transflag(Object &pose_ob, bPoseChannel &pose_bone)
578{
579 int cleared = 0;
580 animrig::pose_bone_descendent_iterator(pose_ob, pose_bone, [&](bPoseChannel &child) {
581 if (&child == &pose_bone) {
582 return;
583 }
584 if (child.runtime.flag & POSE_RUNTIME_TRANSFORM) {
586 cleared++;
587 }
588 });
589 return cleared;
590}
591
592/* Updates all `POSE_RUNTIME_TRANSFORM` flags.
593 * Returns total number of bones with `POSE_RUNTIME_TRANSFORM`.
594 * NOTE: `transform_convert_pose_transflags_update` has a similar logic. */
595static int armature_bone_transflags_update(Object &ob, bArmature *arm, ListBaseT<bPoseChannel> *lb)
596{
597 int total = 0;
598
599 for (bPoseChannel &pchan : *lb) {
600 pchan.runtime.flag &= ~POSE_RUNTIME_TRANSFORM;
601 if (!ANIM_bone_in_visible_collection(arm, pchan.bone_get(ob))) {
602 continue;
603 }
604 if (pchan.flag & POSE_SELECTED) {
605 pchan.runtime.flag |= POSE_RUNTIME_TRANSFORM;
606 total++;
607 }
608 }
609
610 /* No transform on children if any parent bone is selected. */
611 for (bPoseChannel &pchan : *lb) {
612 if (pchan.runtime.flag & POSE_RUNTIME_TRANSFORM) {
613 total -= bone_children_clear_transflag(ob, pchan);
614 }
615 }
616 return total;
617}
618
619void calc_orientation_from_type(const bContext *C, float r_mat[3][3])
620{
621 ARegion *region = CTX_wm_region(C);
622 const Main *bmain = CTX_data_main(C);
623 Scene *scene = CTX_data_scene(C);
624 ViewLayer *view_layer = CTX_data_view_layer(C);
625 Object *obedit = CTX_data_edit_object(C);
626 View3D *v3d = CTX_wm_view3d(C);
627 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
628 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
630 const short orient_index = BKE_scene_orientation_get_index(scene, SCE_ORIENT_DEFAULT);
631 const int pivot_point = scene->toolsettings->transform_pivot_point;
632
634 *bmain, scene, view_layer, v3d, rv3d, ob, obedit, orient_index, pivot_point, r_mat);
635}
636
637static void handle_armature_parent_orientation(Object *ob, float r_mat[3][3])
638{
639 bPoseChannel *active_pchan = BKE_pose_channel_active(ob, false);
640
641 /* Check if target bone is a child. */
642 if (active_pchan && active_pchan->parent) {
643 /* For child, show parent local regardless if "local location" is set for parent bone. */
645 float ob_orientations_mat[3][3];
646 transform_orientations_create_from_axis(ob_orientations_mat,
647 UNPACK3(ob->object_to_world().ptr()));
648 mul_m3_m3_pre(r_mat, ob_orientations_mat);
649 return;
650 }
651
652 /* For root, use local transform of armature object. */
653 transform_orientations_create_from_axis(r_mat, UNPACK3(ob->object_to_world().ptr()));
654}
655
656static void handle_object_parent_orientation(Object *ob, float r_mat[3][3])
657{
658 /* If object has parent, then orient to parent. */
659 if (ob->parent) {
660 transform_orientations_create_from_axis(r_mat, UNPACK3(ob->parent->object_to_world().ptr()));
661 }
662 else {
663 /* If object doesn't have parent, then orient to world. */
664 unit_m3(r_mat);
665 }
666}
667
669 const Scene *scene,
670 ViewLayer *view_layer,
671 const View3D *v3d,
672 const RegionView3D *rv3d,
673 Object *ob,
674 Object *obedit,
675 const short orientation_index,
676 const int pivot_point,
677 float r_mat[3][3])
678{
679 switch (orientation_index) {
680 case V3D_ORIENT_GIMBAL: {
681
682 if (ob) {
683 if (ob->mode & OB_MODE_POSE) {
685
686 if (pchan && gimbal_axis_pose(ob, pchan, r_mat)) {
687 break;
688 }
689 }
690 else {
691 if (gimbal_axis_object(ob, r_mat)) {
692 break;
693 }
694 }
695 }
696 /* If not gimbal, fall through to normal. */
698 }
699 case V3D_ORIENT_PARENT: {
700 if (ob) {
701 if (ob->mode & OB_MODE_POSE) {
703 break;
704 }
706 break;
707 }
708 /* No break; we define 'parent' as 'normal' otherwise. */
710 }
711 case V3D_ORIENT_NORMAL: {
712 if (obedit || (ob && ob->mode & OB_MODE_POSE)) {
714 bmain, scene, view_layer, v3d, ob, obedit, pivot_point, r_mat);
715 break;
716 }
717 /* No break we define 'normal' as 'local' in Object mode. */
719 }
720 case V3D_ORIENT_LOCAL: {
721 if (ob) {
722 if (ob->mode & OB_MODE_POSE) {
723 /* Each bone moves on its own local axis, but to avoid confusion,
724 * use the active bone's axis for display #33575, this works as expected on a single
725 * bone and users who select many bones will understand what's going on and what local
726 * means when they start transforming. */
728 bmain, scene, view_layer, v3d, ob, obedit, pivot_point, r_mat);
729 }
730 else {
731 transform_orientations_create_from_axis(r_mat, UNPACK3(ob->object_to_world().ptr()));
732 }
733 break;
734 }
735 /* If not local, fall through to global. */
737 }
738 case V3D_ORIENT_GLOBAL: {
739 unit_m3(r_mat);
740 break;
741 }
742 case V3D_ORIENT_VIEW: {
743 if (rv3d != nullptr) {
744 copy_m3_m4(r_mat, rv3d->viewinv);
745 normalize_m3(r_mat);
746 }
747 else {
748 unit_m3(r_mat);
749 }
750 break;
751 }
752 case V3D_ORIENT_CURSOR: {
753 copy_m3_m3(r_mat, scene->cursor.matrix<float3x3>().ptr());
754 break;
755 }
757 /* Do nothing. */;
758 break;
759 }
761 default: {
762 BLI_assert(orientation_index >= V3D_ORIENT_CUSTOM);
763 int orientation_index_custom = orientation_index - V3D_ORIENT_CUSTOM;
765 scene, orientation_index_custom);
766 applyTransformOrientation(custom_orientation, r_mat, nullptr);
767 break;
768 }
769 }
770
771 return orientation_index;
772}
773
775 TransInfo *t,
776 short orient_index,
777 const float custom[3][3],
778 float r_spacemtx[3][3])
779{
780 if (orient_index == V3D_ORIENT_CUSTOM_MATRIX) {
781 copy_m3_m3(r_spacemtx, custom);
783 }
784
786 Scene *scene = t->scene;
787 Strip *strip = seq::select_active_get(scene);
788 if (strip && strip->data->transform && orient_index == V3D_ORIENT_LOCAL) {
791 r_spacemtx, 'Z', strip->data->transform->rotation * mirror[0] * mirror[1]);
792 return orient_index;
793 }
794 }
795
797 Object *obedit = CTX_data_edit_object(C);
798 Scene *scene = t->scene;
799 View3D *v3d = nullptr;
800 RegionView3D *rv3d = nullptr;
801
802 if ((t->spacetype == SPACE_VIEW3D) && t->region && (t->region->regiontype == RGN_TYPE_WINDOW)) {
803 v3d = static_cast<View3D *>(t->view);
804 rv3d = static_cast<RegionView3D *>(t->region->regiondata);
805
806 if (ob && (ob->mode & OB_MODE_ALL_WEIGHT_PAINT) && !(t->options & CTX_PAINT_CURVE)) {
808 if (ob_armature) {
809 /* The armature matrix is used for GIMBAL, NORMAL and LOCAL orientations. */
810 ob = ob_armature;
811 }
812 }
813 }
814
815 const short orient_index_result = calc_orientation_from_type_ex(
816 *t->bmain, scene, t->view_layer, v3d, rv3d, ob, obedit, orient_index, t->around, r_spacemtx);
817
818 if (rv3d && (t->options & CTX_PAINT_CURVE)) {
819 /* Screen space in the 3d region. */
820 if (orient_index_result == V3D_ORIENT_VIEW) {
821 unit_m3(r_spacemtx);
822 }
823 else {
824 mul_m3_m4m3(r_spacemtx, rv3d->viewmat, r_spacemtx);
825 normalize_m3(r_spacemtx);
826 }
827 }
828
829 return orient_index_result;
830}
831
832const char *transform_orientations_spacename_get(TransInfo *t, const short orient_type)
833{
834 switch (orient_type) {
836 return RPT_("global");
838 return RPT_("gimbal");
840 return RPT_("normal");
841 case V3D_ORIENT_LOCAL:
842 return RPT_("local");
843 case V3D_ORIENT_VIEW:
844 return RPT_("view");
846 return RPT_("cursor");
848 return RPT_("parent");
850 return RPT_("custom");
852 default:
853 BLI_assert(orient_type >= V3D_ORIENT_CUSTOM);
855 t->scene, orient_type - V3D_ORIENT_CUSTOM);
856 return ts->name;
857 }
858}
859
860void transform_orientations_current_set(TransInfo *t, const short orient_index)
861{
862 const short orientation = t->orient[orient_index].type;
863 const char *spacename = transform_orientations_spacename_get(t, orientation);
864
865 STRNCPY_UTF8(t->spacename, spacename);
866 copy_m3_m3(t->spacemtx, t->orient[orient_index].matrix);
868 t->orient_curr = eTOType(orient_index);
869}
870
875 BMesh *bm, BMElem **elems, const uint n, const BMIterType itype, const char htype)
876{
877 BMIter iter;
878 BMElem *ele;
879 uint i;
880
883
884 if (!bm->selected.is_empty()) {
885 /* Quick check. */
886 i = 0;
887 for (BMEditSelection &ese : bm->selected.items_reversed()) {
888 /* Shouldn't need this check. */
889 if (BM_elem_flag_test(ese.ele, BM_ELEM_SELECT)) {
890
891 /* Only use contiguous selection. */
892 if (ese.htype != htype) {
893 i = 0;
894 break;
895 }
896
897 elems[i++] = ese.ele;
898 if (n == i) {
899 break;
900 }
901 }
902 else {
903 BLI_assert(0);
904 }
905 }
906
907 if (i == 0) {
908 /* Pass. */
909 }
910 else if (i == n) {
911 return i;
912 }
913 }
914
915 i = 0;
916 BM_ITER_MESH (ele, &iter, bm, itype) {
917 BLI_assert(ele->head.htype == htype);
919 elems[i++] = ele;
920 if (n == i) {
921 break;
922 }
923 }
924 }
925
926 return i;
927}
928
930{
932 reinterpret_cast<BMElem **>(elems),
933 min_ii(n, bm->totvertsel),
935 BM_VERT);
936}
938{
940 reinterpret_cast<BMElem **>(elems),
941 min_ii(n, bm->totedgesel),
943 BM_EDGE);
944}
945#if 0
946static uint bm_mesh_faces_select_get_n(BMesh *bm, BMVert **elems, const uint n)
947{
950}
951#endif
952
954 const Scene *scene,
955 ViewLayer *view_layer,
956 const View3D *v3d,
957 Object *ob,
958 Object *obedit,
959 const short around,
960 float r_normal[3],
961 float r_plane[3])
962{
964 const bool activeOnly = (around == V3D_AROUND_ACTIVE);
965
966 zero_v3(r_normal);
967 zero_v3(r_plane);
968
969 if (obedit) {
970 float imat[3][3], mat[3][3];
971
972 /* We need the transpose of the inverse for a normal... */
973 copy_m3_m4(imat, ob->object_to_world().ptr());
974
975 invert_m3_m3(mat, imat);
976 transpose_m3(mat);
977
978 ob = obedit;
979
980 if (ob->type == OB_MESH) {
982 BMEditSelection ese;
983
984 /* Use last selected with active. */
985 if (activeOnly && BM_select_history_active_get(bm, &ese)) {
986 BM_editselection_normal(&ese, r_normal);
987 BM_editselection_plane(&ese, r_plane);
988
989 switch (ese.htype) {
990 case BM_VERT:
992 break;
993 case BM_EDGE:
995 break;
996 case BM_FACE:
998 break;
999 }
1000 }
1001 else {
1002 if (bm->totfacesel >= 1) {
1003 BMFace *efa;
1004 BMIter iter;
1005
1006 float normal[3] = {0.0f};
1007 float plane_pair[2][3] = {{0.0f}};
1008 int face_count = 0;
1009
1010 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
1012 float tangent_pair[2][3];
1013 BM_face_calc_tangent_pair_auto(efa, tangent_pair[0], tangent_pair[1]);
1014 add_v3_v3(normal, efa->no);
1015 add_v3_v3(plane_pair[0], tangent_pair[0]);
1016 add_v3_v3(plane_pair[1], tangent_pair[1]);
1017 face_count++;
1018 }
1019 }
1020
1021 /* Pick the best plane (least likely to be co-linear),
1022 * since this can result in failure to construct a usable matrix, see: #96535. */
1023 int plane_index;
1024 if (face_count == 1) {
1025 /* Special case so a single face always matches
1026 * the active-element orientation, see: #134948. */
1027 plane_index = 0;
1028 }
1029 else {
1030 float normal_unit[3];
1031 float plane_unit_pair[2][3], plane_ortho_pair[2][3];
1032
1033 normalize_v3_v3(normal_unit, normal);
1034 normalize_v3_v3(plane_unit_pair[0], plane_pair[0]);
1035 normalize_v3_v3(plane_unit_pair[1], plane_pair[1]);
1036
1037 cross_v3_v3v3(plane_ortho_pair[0], normal_unit, plane_unit_pair[0]);
1038 cross_v3_v3v3(plane_ortho_pair[1], normal_unit, plane_unit_pair[1]);
1039
1040 plane_index = (len_squared_v3(plane_ortho_pair[0]) >
1041 len_squared_v3(plane_ortho_pair[1])) ?
1042 0 :
1043 1;
1044 }
1045
1046 add_v3_v3(r_normal, normal);
1047 add_v3_v3(r_plane, plane_pair[plane_index]);
1048
1050 }
1051 else if (bm->totvertsel == 3) {
1052 BMVert *v_tri[3];
1053
1054 if (bm_mesh_verts_select_get_n(bm, v_tri, 3) == 3) {
1055 BMEdge *e = nullptr;
1056 float no_test[3];
1057
1058 normal_tri_v3(r_normal, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co);
1059
1060 /* Check if the normal is pointing opposite to vert normals. */
1061 no_test[0] = v_tri[0]->no[0] + v_tri[1]->no[0] + v_tri[2]->no[0];
1062 no_test[1] = v_tri[0]->no[1] + v_tri[1]->no[1] + v_tri[2]->no[1];
1063 no_test[2] = v_tri[0]->no[2] + v_tri[1]->no[2] + v_tri[2]->no[2];
1064 if (dot_v3v3(no_test, r_normal) < 0.0f) {
1065 negate_v3(r_normal);
1066 }
1067
1068 if (bm->totedgesel >= 1) {
1069 /* Find an edge that's a part of v_tri (no need to search all edges). */
1070 float e_length;
1071 int j;
1072
1073 for (j = 0; j < 3; j++) {
1074 BMEdge *e_test = BM_edge_exists(v_tri[j], v_tri[(j + 1) % 3]);
1075 if (e_test && BM_elem_flag_test(e_test, BM_ELEM_SELECT)) {
1076 const float e_test_length = BM_edge_calc_length_squared(e_test);
1077 if ((e == nullptr) || (e_length < e_test_length)) {
1078 e = e_test;
1079 e_length = e_test_length;
1080 }
1081 }
1082 }
1083 }
1084
1085 if (e) {
1086 BMVert *v_pair[2];
1087 if (BM_edge_is_boundary(e)) {
1088 BM_edge_ordered_verts(e, &v_pair[0], &v_pair[1]);
1089 }
1090 else {
1091 v_pair[0] = e->v1;
1092 v_pair[1] = e->v2;
1093 }
1094 sub_v3_v3v3(r_plane, v_pair[0]->co, v_pair[1]->co);
1095 }
1096 else {
1097 BM_vert_tri_calc_tangent_from_edge(v_tri, r_plane);
1098 }
1099 }
1100 else {
1101 BLI_assert(0);
1102 }
1103
1105 }
1106 else if (bm->totedgesel == 1 || bm->totvertsel == 2) {
1107 BMVert *v_pair[2] = {nullptr, nullptr};
1108 BMEdge *eed = nullptr;
1109
1110 if (bm->totedgesel == 1) {
1111 if (bm_mesh_edges_select_get_n(bm, &eed, 1) == 1) {
1112 v_pair[0] = eed->v1;
1113 v_pair[1] = eed->v2;
1114 }
1115 }
1116 else {
1117 BLI_assert(bm->totvertsel == 2);
1118 bm_mesh_verts_select_get_n(bm, v_pair, 2);
1119 }
1120
1121 /* Should never fail. */
1122 if (v_pair[0] && v_pair[1]) [[likely]] {
1123 bool v_pair_swap = false;
1134
1135 /* Be deterministic where possible and ensure `v_pair[0]` is active. */
1136 if (BM_mesh_active_vert_get(bm) == v_pair[1]) {
1137 v_pair_swap = true;
1138 }
1139 else if (eed && BM_edge_is_boundary(eed)) {
1140 /* Predictable direction for boundary edges. */
1141 if (eed->l->v != v_pair[0]) {
1142 v_pair_swap = true;
1143 }
1144 }
1145
1146 if (v_pair_swap) {
1147 std::swap(v_pair[0], v_pair[1]);
1148 }
1149
1150 add_v3_v3v3(r_normal, v_pair[1]->no, v_pair[0]->no);
1151 sub_v3_v3v3(r_plane, v_pair[1]->co, v_pair[0]->co);
1152
1153 if (normalize_v3(r_plane) != 0.0f) {
1154 /* For edges it'd important the resulting matrix can rotate around the edge,
1155 * project onto the plane so we can use a fallback value. */
1156 project_plane_normalized_v3_v3v3(r_normal, r_normal, r_plane);
1157 if (normalize_v3(r_normal) == 0.0f) [[unlikely]] {
1158 /* In the case the normal and plane are aligned,
1159 * use a fallback normal which is orthogonal to the plane. */
1160 ortho_v3_v3(r_normal, r_plane);
1161 }
1162 }
1163 }
1164
1166 }
1167 else if (bm->totvertsel == 1) {
1168 BMVert *v = nullptr;
1169
1170 if (bm_mesh_verts_select_get_n(bm, &v, 1) == 1) {
1171 copy_v3_v3(r_normal, v->no);
1172 BMEdge *e_pair[2];
1173
1174 if (BM_vert_edge_pair(v, &e_pair[0], &e_pair[1])) {
1175 bool v_pair_swap = false;
1176 BMVert *v_pair[2] = {
1177 BM_edge_other_vert(e_pair[0], v),
1178 BM_edge_other_vert(e_pair[1], v),
1179 };
1180 float dir_pair[2][3];
1181
1182 if (BM_edge_is_boundary(e_pair[0])) {
1183 if (e_pair[0]->l->v != v) {
1184 v_pair_swap = true;
1185 }
1186 }
1187 else {
1188 if (BM_edge_calc_length_squared(e_pair[0]) <
1189 BM_edge_calc_length_squared(e_pair[1]))
1190 {
1191 v_pair_swap = true;
1192 }
1193 }
1194
1195 if (v_pair_swap) {
1196 std::swap(v_pair[0], v_pair[1]);
1197 }
1198
1199 sub_v3_v3v3(dir_pair[0], v->co, v_pair[0]->co);
1200 sub_v3_v3v3(dir_pair[1], v_pair[1]->co, v->co);
1201 normalize_v3(dir_pair[0]);
1202 normalize_v3(dir_pair[1]);
1203
1204 add_v3_v3v3(r_plane, dir_pair[0], dir_pair[1]);
1205 }
1206 }
1207
1209 }
1210 else if (bm->totvertsel > 3) {
1211 BMIter iter;
1212 BMVert *v;
1213
1214 zero_v3(r_normal);
1215
1216 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1218 add_v3_v3(r_normal, v->no);
1219 }
1220 }
1221 normalize_v3(r_normal);
1223 }
1224 }
1225
1226 /* Not needed but this matches 2.68 and older behavior. */
1227 negate_v3(r_plane);
1228
1229 } /* End edit-mesh. */
1230 else if (ELEM(obedit->type, OB_CURVES_LEGACY, OB_SURF)) {
1231 Curve *cu = id_cast<Curve *>(obedit->data);
1232 Nurb *nu = nullptr;
1233 int a;
1234 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
1235
1236 void *vert_act = nullptr;
1237 if (activeOnly && BKE_curve_nurb_vert_active_get(cu, &nu, &vert_act)) {
1238 if (nu->type == CU_BEZIER) {
1239 BezTriple *bezt = static_cast<BezTriple *>(vert_act);
1240 BKE_nurb_bezt_calc_normal(nu, bezt, r_normal);
1241 BKE_nurb_bezt_calc_plane(nu, bezt, r_plane);
1242 }
1243 else {
1244 BPoint *bp = static_cast<BPoint *>(vert_act);
1245 BKE_nurb_bpoint_calc_normal(nu, bp, r_normal);
1246 BKE_nurb_bpoint_calc_plane(nu, bp, r_plane);
1247 }
1248 }
1249 else {
1250 const bool use_handle = v3d ? (v3d->overlay.handle_display != CURVE_HANDLE_NONE) : true;
1251
1252 for (nu = nurbs->first(); nu; nu = nu->next) {
1253 /* Only bezier has a normal. */
1254 if (nu->type == CU_BEZIER) {
1255 BezTriple *bezt = nu->bezt;
1256 a = nu->pntsu;
1257 while (a--) {
1258 short flag = 0;
1259
1260#define SEL_F1 (1 << 0)
1261#define SEL_F2 (1 << 1)
1262#define SEL_F3 (1 << 2)
1263
1264 if (use_handle) {
1265 if (bezt->f1 & SELECT) {
1266 flag |= SEL_F1;
1267 }
1268 if (bezt->f2 & SELECT) {
1269 flag |= SEL_F2;
1270 }
1271 if (bezt->f3 & SELECT) {
1272 flag |= SEL_F3;
1273 }
1274 }
1275 else {
1276 flag = (bezt->f2 & SELECT) ? (SEL_F1 | SEL_F2 | SEL_F3) : 0;
1277 }
1278
1279 /* Exception. */
1280 if (flag) {
1281 float tvec[3];
1282 if ((around == V3D_AROUND_LOCAL_ORIGINS) ||
1283 ELEM(flag, SEL_F2, SEL_F1 | SEL_F3, SEL_F1 | SEL_F2 | SEL_F3))
1284 {
1285 BKE_nurb_bezt_calc_normal(nu, bezt, tvec);
1286 add_v3_v3(r_normal, tvec);
1287 }
1288 else {
1289 /* Ignore `bezt->f2` in this case. */
1290 if (flag & SEL_F1) {
1291 sub_v3_v3v3(tvec, bezt->vec[0], bezt->vec[1]);
1292 normalize_v3(tvec);
1293 add_v3_v3(r_normal, tvec);
1294 }
1295 if (flag & SEL_F3) {
1296 sub_v3_v3v3(tvec, bezt->vec[1], bezt->vec[2]);
1297 normalize_v3(tvec);
1298 add_v3_v3(r_normal, tvec);
1299 }
1300 }
1301
1302 BKE_nurb_bezt_calc_plane(nu, bezt, tvec);
1303 add_v3_v3(r_plane, tvec);
1304 }
1305
1306#undef SEL_F1
1307#undef SEL_F2
1308#undef SEL_F3
1309
1310 bezt++;
1311 }
1312 }
1313 else if (nu->bp && (nu->pntsv == 1)) {
1314 BPoint *bp = nu->bp;
1315 a = nu->pntsu;
1316 while (a--) {
1317 if (bp->f1 & SELECT) {
1318 float tvec[3];
1319
1320 BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp);
1321 BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp);
1322
1323 const bool is_prev_sel = bp_prev && (bp_prev->f1 & SELECT);
1324 const bool is_next_sel = bp_next && (bp_next->f1 & SELECT);
1325 if (is_prev_sel == false && is_next_sel == false) {
1326 /* Isolated, add based on surrounding. */
1327 BKE_nurb_bpoint_calc_normal(nu, bp, tvec);
1328 add_v3_v3(r_normal, tvec);
1329 }
1330 else if (is_next_sel) {
1331 /* A segment, add the edge normal. */
1332 sub_v3_v3v3(tvec, bp->vec, bp_next->vec);
1333 normalize_v3(tvec);
1334 add_v3_v3(r_normal, tvec);
1335 }
1336
1337 BKE_nurb_bpoint_calc_plane(nu, bp, tvec);
1338 add_v3_v3(r_plane, tvec);
1339 }
1340 bp++;
1341 }
1342 }
1343 }
1344 }
1345
1346 if (!is_zero_v3(r_normal)) {
1348 }
1349 }
1350 else if (obedit->type == OB_MBALL) {
1351 MetaBall *mb = id_cast<MetaBall *>(obedit->data);
1352 MetaElem *ml;
1353 bool ok = false;
1354 float tmat[3][3];
1355
1356 if (activeOnly && (ml = mb->lastelem)) {
1357 quat_to_mat3(tmat, ml->quat);
1358 add_v3_v3(r_normal, tmat[2]);
1359 add_v3_v3(r_plane, tmat[1]);
1360 ok = true;
1361 }
1362 else {
1363 for (MetaElem &ml : *mb->editelems) {
1364 if (ml.flag & SELECT) {
1365 quat_to_mat3(tmat, ml.quat);
1366 add_v3_v3(r_normal, tmat[2]);
1367 add_v3_v3(r_plane, tmat[1]);
1368 ok = true;
1369 }
1370 }
1371 }
1372
1373 if (ok) {
1374 if (!is_zero_v3(r_plane)) {
1376 }
1377 }
1378 }
1379 else if (obedit->type == OB_ARMATURE) {
1380 bArmature *arm = id_cast<bArmature *>(obedit->data);
1381 EditBone *ebone;
1382 bool ok = false;
1383 float tmat[3][3];
1384
1385 if (activeOnly && (ebone = arm->act_edbone)) {
1386 ED_armature_ebone_to_mat3(ebone, tmat);
1387 add_v3_v3(r_normal, tmat[2]);
1388 add_v3_v3(r_plane, tmat[1]);
1389 ok = true;
1390 }
1391 else {
1392 /* When we only have the root/tip are selected. */
1393 bool fallback_ok = false;
1394 float fallback_normal[3];
1395 float fallback_plane[3];
1396
1397 zero_v3(fallback_normal);
1398 zero_v3(fallback_plane);
1399
1400 for (EditBone &ebone : *arm->edbo) {
1401 if (animrig::bone_is_visible(arm, &ebone)) {
1402 if (ebone.flag & BONE_SELECTED) {
1403 ED_armature_ebone_to_mat3(&ebone, tmat);
1404 add_v3_v3(r_normal, tmat[2]);
1405 add_v3_v3(r_plane, tmat[1]);
1406 ok = true;
1407 }
1408 else if ((ok == false) && ((ebone.flag & BONE_TIPSEL) ||
1409 ((ebone.flag & BONE_ROOTSEL) &&
1410 (ebone.parent && ebone.flag & BONE_CONNECTED) == false)))
1411 {
1412 ED_armature_ebone_to_mat3(&ebone, tmat);
1413 add_v3_v3(fallback_normal, tmat[2]);
1414 add_v3_v3(fallback_plane, tmat[1]);
1415 fallback_ok = true;
1416 }
1417 }
1418 }
1419 if ((ok == false) && fallback_ok) {
1420 ok = true;
1421 copy_v3_v3(r_normal, fallback_normal);
1422 copy_v3_v3(r_plane, fallback_plane);
1423 }
1424 }
1425
1426 if (ok) {
1427 if (!is_zero_v3(r_plane)) {
1429 }
1430 }
1431 }
1432
1433 /* Vectors from edges don't need the special transpose inverse multiplication. */
1434 if (result == ORIENTATION_EDGE) {
1435 float tvec[3];
1436
1437 mul_mat3_m4_v3(ob->object_to_world().ptr(), r_normal);
1438 mul_mat3_m4_v3(ob->object_to_world().ptr(), r_plane);
1439
1440 /* Align normal to edge direction (so normal is perpendicular to the plane).
1441 * 'ORIENTATION_EDGE' will do the other way around.
1442 * This has to be done **after** applying obmat, see #45775! */
1443 project_v3_v3v3(tvec, r_normal, r_plane);
1444 sub_v3_v3(r_normal, tvec);
1445 }
1446 else {
1447 mul_m3_v3(mat, r_normal);
1448 mul_m3_v3(mat, r_plane);
1449 }
1450 }
1451 else if (ob && (ob->mode & OB_MODE_POSE)) {
1452 bArmature *arm = id_cast<bArmature *>(ob->data);
1453 bPoseChannel *pchan;
1454 float imat[3][3], mat[3][3];
1455 bool ok = false;
1456
1457 if (activeOnly && (pchan = BKE_pose_channel_active_if_bonecoll_visible(ob))) {
1458 float pose_mat[3][3];
1459 BKE_pose_channel_transform_orientation(arm, pchan, pose_mat);
1460
1461 add_v3_v3(r_normal, pose_mat[2]);
1462 add_v3_v3(r_plane, pose_mat[1]);
1463 ok = true;
1464 }
1465 else {
1466 const int transformed_len = armature_bone_transflags_update(*ob, arm, &ob->pose->chanbase);
1467 if (transformed_len) {
1468 /* Use channels to get stats. */
1469 for (bPoseChannel &pchan : ob->pose->chanbase) {
1470 if (pchan.runtime.flag & POSE_RUNTIME_TRANSFORM) {
1471 float pose_mat[3][3];
1472 BKE_pose_channel_transform_orientation(arm, &pchan, pose_mat);
1473
1474 add_v3_v3(r_normal, pose_mat[2]);
1475 add_v3_v3(r_plane, pose_mat[1]);
1476 }
1477 }
1478 ok = true;
1479 }
1480 }
1481
1482 /* Use for both active & all. */
1483 if (ok) {
1484 /* We need the transpose of the inverse for a normal. */
1485 copy_m3_m4(imat, ob->object_to_world().ptr());
1486
1487 invert_m3_m3(mat, imat);
1488 transpose_m3(mat);
1489 mul_m3_v3(mat, r_normal);
1490 mul_m3_v3(mat, r_plane);
1491
1493 }
1494 }
1495 else {
1496 /* We need the one selected object, if its not active. */
1497 if (ob != nullptr) {
1498 bool ok = false;
1499 if (activeOnly || (ob->mode & (OB_MODE_ALL_PAINT_MESH | OB_MODE_PARTICLE_EDIT))) {
1500 /* Ignore selection state. */
1501 ok = true;
1502 }
1503 else {
1504 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
1505 Base *base = BKE_view_layer_base_find(view_layer, ob);
1506 if (base == nullptr) [[unlikely]] {
1507 /* This is very unlikely, if it happens allow the value to be set since the caller
1508 * may have taken the object from outside this view-layer. */
1509 ok = true;
1510 }
1511 else if (BASE_SELECTED(v3d, base)) {
1512 ok = true;
1513 }
1514 }
1515
1516 if (ok) {
1517 copy_v3_v3(r_normal, ob->object_to_world().ptr()[2]);
1518 copy_v3_v3(r_plane, ob->object_to_world().ptr()[1]);
1519 }
1520 }
1522 }
1523
1524 normalize_v3(r_normal);
1525 normalize_v3(r_plane);
1526
1527 return result;
1528}
1529
1530int getTransformOrientation(const bContext *C, float r_normal[3], float r_plane[3])
1531{
1533 Object *obedit = CTX_data_edit_object(C);
1534
1535 /* Dummy value, not #V3D_AROUND_ACTIVE and not #V3D_AROUND_LOCAL_ORIGINS. */
1536 short around = V3D_AROUND_CENTER_BOUNDS;
1537
1538 const Main *bmain = CTX_data_main(C);
1539 const Scene *scene = CTX_data_scene(C);
1540 ViewLayer *view_layer = CTX_data_view_layer(C);
1541 View3D *v3d = CTX_wm_view3d(C);
1542
1544 *bmain, scene, view_layer, v3d, obact, obedit, around, r_normal, r_plane);
1545}
1546
1548 const Scene *scene,
1549 ViewLayer *view_layer,
1550 const View3D *v3d,
1551 Object *ob,
1552 Object *obedit,
1553 const short around,
1554 float r_orientation_mat[3][3])
1555{
1556 float normal[3] = {0.0, 0.0, 0.0};
1557 float plane[3] = {0.0, 0.0, 0.0};
1558
1559 int type;
1560
1562 bmain, scene, view_layer, v3d, ob, obedit, around, normal, plane);
1563
1564 /* Fallback, when the plane can't be calculated. */
1565 if (ORIENTATION_USE_PLANE(type) && is_zero_v3(plane)) {
1566 type = ORIENTATION_VERT;
1567 }
1568
1569 switch (type) {
1570 case ORIENTATION_NORMAL:
1571 if (createSpaceNormalTangent(r_orientation_mat, normal, plane) == 0) {
1572 type = ORIENTATION_NONE;
1573 }
1574 break;
1575 case ORIENTATION_VERT:
1576 if (createSpaceNormal(r_orientation_mat, normal) == 0) {
1577 type = ORIENTATION_NONE;
1578 }
1579 break;
1580 case ORIENTATION_EDGE:
1581 if (createSpaceNormalTangent(r_orientation_mat, normal, plane) == 0) {
1582 type = ORIENTATION_NONE;
1583 }
1584 break;
1585 case ORIENTATION_FACE:
1586 if (createSpaceNormalTangent(r_orientation_mat, normal, plane) == 0) {
1587 type = ORIENTATION_NONE;
1588 }
1589 break;
1590 default:
1592 break;
1593 }
1594
1595 if (type == ORIENTATION_NONE) {
1596 unit_m3(r_orientation_mat);
1597 }
1598}
1599
1600} // namespace blender::ed::transform
Functions to deal with Armatures.
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
Blender kernel action and pose functionality.
#define SELECT
Definition BKE_mask.hh:19
#define ATTR_FALLTHROUGH
#define BLI_ASSERT_UNIT_V3(v)
#define M_PI_2
#define STRNCPY_UTF8(dst, src)
#define RPT_(msgid)
#define CTX_DATA_(context, msgid)
#define BLT_I18NCONTEXT_ID_SCENE
#define DATA_(msgid)
#define MAX_NAME
Definition DNA_defs.h:63
#define OB_MODE_ALL_PAINT_MESH
#define OB_MODE_ALL_WEIGHT_PAINT
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UNPACK3(a)
#define ELEM(...)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_MESH(ele, iter, bm, itype)
void activate(bool forceActivation=false) const
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr const char * c_str() const
const T & data(const Action &owning_action) const
#define SEL_F1
Definition curve.cc:4046
#define SEL_F2
Definition curve.cc:4047
#define SEL_F3
Definition curve.cc:4048
#define offsetof(t, d)
bool is_zero(float3x3 a)
bool bone_is_visible(const bArmature *armature, const Bone *bone)
constexpr char bone_default_name[]
void pose_bone_descendent_iterator(Object &pose_ob, bPoseChannel &pchan, FunctionRef< void(bPoseChannel &child_bone)> callback)
void createSpaceNormalTangent_or_fallback(float mat[3][3], const float normal[3], const float tangent[3])
static bool test_rotmode_euler(short rotmode)
TransformOrientation * addMatrixSpace(bContext *C, float mat[3][3], const char *name, const bool overwrite)
static void uniqueOrientationName(ListBaseT< TransformOrientation > *lb, char *name)
static int armature_bone_transflags_update(Object &ob, bArmature *arm, ListBaseT< bPoseChannel > *lb)
static void axis_angle_to_gimbal_axis(float gmat[3][3], const float axis[3], const float angle)
int getTransformOrientation(const bContext *C, float r_normal[3], float r_plane[3])
static void handle_object_parent_orientation(Object *ob, float r_mat[3][3])
bool gimbal_axis_pose(Object *ob, const bPoseChannel *pchan, float gmat[3][3])
bool BIF_createTransformOrientation(bContext *C, ReportList *reports, const char *name, bool use_view, bool activate, bool overwrite)
static TransformOrientation * createViewSpace(bContext *C, ReportList *, const char *name, const bool overwrite)
static TransformOrientation * createMeshSpace(bContext *C, ReportList *reports, const char *name, const bool overwrite)
static int bone_children_clear_transflag(Object &pose_ob, bPoseChannel &pose_bone)
void BIF_clearTransformOrientation(bContext *C)
static TransformOrientation * createObjectSpace(bContext *C, ReportList *, const char *name, const bool overwrite)
void BIF_removeTransformOrientationIndex(bContext *C, int index)
static TransformOrientation * findOrientationName(ListBaseT< TransformOrientation > *lb, const char *name)
Object * transform_object_deform_pose_armature_get(const TransInfo *t, Object *ob)
bool createSpaceNormal(float mat[3][3], const float normal[3])
int getTransformOrientation_ex(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, Object *ob, Object *obedit, const short around, float r_normal[3], float r_plane[3])
bool createSpaceNormalTangent(float mat[3][3], const float normal[3], const float tangent[3])
short calc_orientation_from_type_ex(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, const RegionView3D *rv3d, Object *ob, Object *obedit, short orientation_index, int pivot_point, float r_mat[3][3])
void applyTransformOrientation(const TransformOrientation *ts, float r_mat[3][3], char r_name[64])
static void handle_armature_parent_orientation(Object *ob, float r_mat[3][3])
static uint bm_mesh_elems_select_get_n__internal(BMesh *bm, BMElem **elems, const uint n, const BMIterType itype, const char htype)
void calc_orientation_from_type(const bContext *C, float r_mat[3][3])
int BIF_countTransformOrientation(const bContext *C)
void BIF_removeTransformOrientation(bContext *C, TransformOrientation *target)
bool transform_orientations_create_from_axis(float mat[3][3], const float x[3], const float y[3], const float z[3])
short transform_orientation_matrix_get(bContext *C, TransInfo *t, short orient_index, const float custom[3][3], float r_spacemtx[3][3])
static uint bm_mesh_edges_select_get_n(BMesh *bm, BMEdge **elems, const uint n)
void ED_getTransformOrientationMatrix(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, Object *ob, Object *obedit, short around, float r_orientation_mat[3][3])
static TransformOrientation * createCurveSpace(bContext *C, ReportList *reports, const char *name, const bool overwrite)
const char * transform_orientations_spacename_get(TransInfo *t, const short orient_type)
static TransformOrientation * createBoneSpace(bContext *C, ReportList *reports, const char *name, const bool overwrite)
bool gimbal_axis_object(Object *ob, float gmat[3][3])
static uint bm_mesh_verts_select_get_n(BMesh *bm, BMVert **elems, const uint n)
void BIF_selectTransformOrientation(bContext *C, TransformOrientation *target)
void transform_orientations_current_set(TransInfo *t, const short orient_index)
float2 image_transform_mirror_factor_get(const Strip *strip)
Strip * select_active_get(const Scene *scene)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void negate_v3_v3(float r[3], const float a[3])
void mul_m3_v3(const float M[3][3], float r[3])
bPoseChannel * BKE_pose_channel_active(Object *ob, bool check_bonecoll)
void mul_m3_m3_pre(float R[3][3], const float A[3][3])
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void copy_m3_m4(float m1[3][3], const float m2[4][4])
@ V3D_ORIENT_CUSTOM_MATRIX
ATTR_WARN_UNUSED_RESULT const BMLoop * l
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void void BKE_reports_prepend(ReportList *reports, const char *prepend)
Definition report.cc:244
void copy_m3_m3(float m1[3][3], const float m2[3][3])
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void mul_m3_m4m3(float R[3][3], const float A[4][4], const float B[3][3])
void normalize_m3(float R[3][3]) ATTR_NONNULL()
@ V3D_AROUND_CENTER_BOUNDS
@ V3D_AROUND_LOCAL_ORIGINS
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
bool ANIM_bone_in_visible_collection(const bArmature *armature, const Bone *bone)
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
int BKE_scene_orientation_get_index(Scene *scene, int slot_index)
Definition scene.cc:2678
void BKE_nurb_bezt_calc_normal(Nurb *nu, BezTriple *bezt, float r_normal[3])
Definition curve.cc:1032
void project_plane_normalized_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:45
void BKE_pose_channel_transform_orientation(const bArmature *arm, const bPoseChannel *pose_bone, float r_pose_orientation[3][3])
Base * CTX_data_active_base(const bContext *C)
void eulO_to_gimbal_axis(float gmat[3][3], const float eul[3], short order)
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ViewLayer * CTX_data_view_layer(const bContext *C)
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
void mul_qt_v3(const float q[4], float r[3])
void quat_to_mat3(float m[3][3], const float q[4])
size_t void BLI_uniquename_cb(FunctionRef< bool(StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
BMesh const char itype
VecBase< float, 2 > float2
void ED_armature_ebone_to_mat3(EditBone *ebone, float r_mat[3][3])
void transpose_m3(float R[3][3])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
@ OB_MODE_PARTICLE_EDIT
Object * CTX_data_edit_object(const bContext *C)
void BM_vert_tri_calc_tangent_from_edge(BMVert *verts[3], float r_tangent[3])
void BM_face_calc_tangent_pair_auto(const BMFace *f, float r_tangent_a[3], float r_tangent_b[3])
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
MINLINE void negate_v3(float r[3])
BLI_INLINE bool BM_edge_is_boundary(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BM_edge_ordered_verts(const BMEdge *edge, BMVert **r_v1, BMVert **r_v2)
RegionView3D * CTX_wm_region_view3d(const bContext *C)
void BM_editselection_plane(BMEditSelection *ese, float r_plane[3])
MINLINE void zero_v3(float r[3])
MatBase< float, 3, 3 > float3x3
void invert_m3_m3_safe_ortho(float inverse[3][3], const float mat[3][3])
TransformOrientation * BKE_scene_transform_orientation_find(const Scene *scene, int index)
Definition scene.cc:3753
void ortho_v3_v3(float out[3], const float v[3])
int BKE_scene_transform_orientation_get_index(const Scene *scene, const TransformOrientation *orientation)
Definition scene.cc:3758
void project_plane_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
void BKE_scene_transform_orientation_remove(Scene *scene, TransformOrientation *orientation)
Definition scene.cc:3733
MINLINE float normalize_v3_v3(float r[3], const float a[3])
bool invert_m3(float mat[3][3])
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
BPoint * BKE_nurb_bpoint_get_prev(Nurb *nu, BPoint *bp)
Definition curve.cc:1010
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
float BM_edge_calc_length_squared(const BMEdge *e)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
void unit_m3(float m[3][3])
bool BKE_curve_nurb_vert_active_get(Curve *cu, Nurb **r_nu, void **r_vert)
Definition curve.cc:5065
void BKE_nurb_bpoint_calc_plane(Nurb *nu, BPoint *bp, float r_plane[3])
Definition curve.cc:1107
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void BKE_nurb_bpoint_calc_normal(Nurb *nu, BPoint *bp, float r_normal[3])
Definition curve.cc:1084
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
bool BM_vert_edge_pair(const BMVert *v, BMEdge **r_e_a, BMEdge **r_e_b)
void zero_m3(float m[3][3])
ListBaseT< Nurb > * BKE_curve_editNurbs_get(Curve *cu)
Definition curve.cc:441
bPoseChannel * BKE_pose_channel_active_if_bonecoll_visible(Object *ob) ATTR_WARN_UNUSED_RESULT
Scene * CTX_data_scene(const bContext *C)
BPoint * BKE_nurb_bpoint_get_next(Nurb *nu, BPoint *bp)
Definition curve.cc:967
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
bool BM_select_history_active_get(BMesh *bm, BMEditSelection *ese)
void BKE_nurb_bezt_calc_plane(Nurb *nu, BezTriple *bezt, float r_plane[3])
Definition curve.cc:1047
MINLINE int min_ii(int a, int b)
Object * CTX_data_active_object(const bContext *C)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
ARegion * CTX_wm_region(const bContext *C)
void BM_editselection_normal(BMEditSelection *ese, float r_normal[3])
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
BMVert * BM_mesh_active_vert_get(BMesh *bm)
View3D * CTX_wm_view3d(const bContext *C)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
Normal to x,y matrix.
void axis_angle_to_mat3_single(float R[3][3], char axis, float angle)
eRegion_Type regiontype
struct BMLoop * l
struct BMVert * v
ListBaseT< BMEditSelection > selected
struct Object * object
eBezTriple_Flag f2
eBezTriple_Flag f3
eBezTriple_Flag f1
struct Bone * parent
char name[258]
Definition DNA_ID.h:433
const c_style_mat & ptr() const
ListBaseT< MetaElem > * editelems
eMetaElem_Flag flag
BezTriple * bezt
struct Nurb * next
struct Object * parent
struct bPose * pose
eRegionView3D_Persp persp
TransformOrientationSlot orientation_slots[4]
ListBaseT< TransformOrientation > transform_spaces
View3DCursor cursor
struct ToolSettings * toolsettings
eHandleDisplay handle_display
View3DOverlay overlay
struct Object * camera
ListBaseT< EditBone > * edbo
struct EditBone * act_edbone
bPoseChannelRuntimeFlag flag
struct bPoseChannel_Runtime runtime
struct bPoseChannel * parent
ListBaseT< bPoseChannel > chanbase
struct blender::ed::transform::TransInfo::@074260112357037217233016050204226071352346123342 orient[3]
#define ORIENTATION_USE_PLANE(ty)