1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
|
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2001-2002 NaN Holding BV. All rights reserved. */
/** \file
* \ingroup edtransform
*/
#include <stdlib.h>
#include "DNA_anim_types.h"
#include "DNA_armature_types.h"
#include "DNA_constraint_types.h"
#include "DNA_gpencil_types.h"
#include "DNA_windowmanager_types.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_string.h"
#include "BKE_constraint.h"
#include "BKE_context.h"
#include "BKE_nla.h"
#include "RNA_access.h"
#include "UI_interface.h"
#include "BLT_translation.h"
#include "transform.h"
#include "transform_convert.h"
#include "transform_orientations.h"
#include "transform_snap.h"
/* Own include. */
#include "transform_mode.h"
eTfmMode transform_mode_really_used(bContext *C, eTfmMode mode)
{
if (mode == TFM_BONESIZE) {
Object *ob = CTX_data_active_object(C);
BLI_assert(ob);
if (ob->type != OB_ARMATURE) {
return TFM_RESIZE;
}
bArmature *arm = ob->data;
if (arm->drawtype == ARM_ENVELOPE) {
return TFM_BONE_ENVELOPE_DIST;
}
}
return mode;
}
bool transdata_check_local_center(const TransInfo *t, short around)
{
return ((around == V3D_AROUND_LOCAL_ORIGINS) &&
((t->options & (CTX_OBJECT | CTX_POSE_BONE)) ||
/* implicit: (t->flag & T_EDIT) */
(ELEM(t->obedit_type, OB_MESH, OB_CURVES_LEGACY, OB_MBALL, OB_ARMATURE, OB_GPENCIL)) ||
(t->spacetype == SPACE_GRAPH) ||
(t->options & (CTX_MOVIECLIP | CTX_MASK | CTX_PAINT_CURVE | CTX_SEQUENCER_IMAGE))));
}
bool transform_mode_is_changeable(const int mode)
{
return ELEM(mode,
TFM_ROTATION,
TFM_RESIZE,
TFM_TRACKBALL,
TFM_TRANSLATION,
TFM_EDGE_SLIDE,
TFM_VERT_SLIDE);
}
/* -------------------------------------------------------------------- */
/** \name Transform Locks
* \{ */
void protectedTransBits(short protectflag, float vec[3])
{
if (protectflag & OB_LOCK_LOCX) {
vec[0] = 0.0f;
}
if (protectflag & OB_LOCK_LOCY) {
vec[1] = 0.0f;
}
if (protectflag & OB_LOCK_LOCZ) {
vec[2] = 0.0f;
}
}
/* this function only does the delta rotation */
static void protectedQuaternionBits(short protectflag, float quat[4], const float oldquat[4])
{
/* check that protection flags are set */
if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0) {
return;
}
if (protectflag & OB_LOCK_ROT4D) {
/* quaternions getting limited as 4D entities that they are... */
if (protectflag & OB_LOCK_ROTW) {
quat[0] = oldquat[0];
}
if (protectflag & OB_LOCK_ROTX) {
quat[1] = oldquat[1];
}
if (protectflag & OB_LOCK_ROTY) {
quat[2] = oldquat[2];
}
if (protectflag & OB_LOCK_ROTZ) {
quat[3] = oldquat[3];
}
}
else {
/* quaternions get limited with euler... (compatibility mode) */
float eul[3], oldeul[3], nquat[4], noldquat[4];
float qlen;
qlen = normalize_qt_qt(nquat, quat);
normalize_qt_qt(noldquat, oldquat);
quat_to_eul(eul, nquat);
quat_to_eul(oldeul, noldquat);
if (protectflag & OB_LOCK_ROTX) {
eul[0] = oldeul[0];
}
if (protectflag & OB_LOCK_ROTY) {
eul[1] = oldeul[1];
}
if (protectflag & OB_LOCK_ROTZ) {
eul[2] = oldeul[2];
}
eul_to_quat(quat, eul);
/* restore original quat size */
mul_qt_fl(quat, qlen);
/* quaternions flip w sign to accumulate rotations correctly */
if ((nquat[0] < 0.0f && quat[0] > 0.0f) || (nquat[0] > 0.0f && quat[0] < 0.0f)) {
mul_qt_fl(quat, -1.0f);
}
}
}
static void protectedRotateBits(short protectflag, float eul[3], const float oldeul[3])
{
if (protectflag & OB_LOCK_ROTX) {
eul[0] = oldeul[0];
}
if (protectflag & OB_LOCK_ROTY) {
eul[1] = oldeul[1];
}
if (protectflag & OB_LOCK_ROTZ) {
eul[2] = oldeul[2];
}
}
/* this function only does the delta rotation */
/* axis-angle is usually internally stored as quats... */
static void protectedAxisAngleBits(
short protectflag, float axis[3], float *angle, const float oldAxis[3], float oldAngle)
{
/* check that protection flags are set */
if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0) {
return;
}
if (protectflag & OB_LOCK_ROT4D) {
/* axis-angle getting limited as 4D entities that they are... */
if (protectflag & OB_LOCK_ROTW) {
*angle = oldAngle;
}
if (protectflag & OB_LOCK_ROTX) {
axis[0] = oldAxis[0];
}
if (protectflag & OB_LOCK_ROTY) {
axis[1] = oldAxis[1];
}
if (protectflag & OB_LOCK_ROTZ) {
axis[2] = oldAxis[2];
}
}
else {
/* axis-angle get limited with euler... */
float eul[3], oldeul[3];
axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, axis, *angle);
axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, oldAxis, oldAngle);
if (protectflag & OB_LOCK_ROTX) {
eul[0] = oldeul[0];
}
if (protectflag & OB_LOCK_ROTY) {
eul[1] = oldeul[1];
}
if (protectflag & OB_LOCK_ROTZ) {
eul[2] = oldeul[2];
}
eulO_to_axis_angle(axis, angle, eul, EULER_ORDER_DEFAULT);
/* When converting to axis-angle,
* we need a special exception for the case when there is no axis. */
if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
/* for now, rotate around y-axis then (so that it simply becomes the roll) */
axis[1] = 1.0f;
}
}
}
void protectedSizeBits(short protectflag, float size[3])
{
if (protectflag & OB_LOCK_SCALEX) {
size[0] = 1.0f;
}
if (protectflag & OB_LOCK_SCALEY) {
size[1] = 1.0f;
}
if (protectflag & OB_LOCK_SCALEZ) {
size[2] = 1.0f;
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Transform Limits
* \{ */
void constraintTransLim(const TransInfo *t, TransData *td)
{
if (td->con) {
const bConstraintTypeInfo *ctiLoc = BKE_constraint_typeinfo_from_type(
CONSTRAINT_TYPE_LOCLIMIT);
const bConstraintTypeInfo *ctiDist = BKE_constraint_typeinfo_from_type(
CONSTRAINT_TYPE_DISTLIMIT);
bConstraintOb cob = {NULL};
bConstraint *con;
float ctime = (float)(t->scene->r.cfra);
/* Make a temporary bConstraintOb for using these limit constraints
* - they only care that cob->matrix is correctly set ;-)
* - current space should be local
*/
unit_m4(cob.matrix);
copy_v3_v3(cob.matrix[3], td->loc);
/* Evaluate valid constraints */
for (con = td->con; con; con = con->next) {
const bConstraintTypeInfo *cti = NULL;
ListBase targets = {NULL, NULL};
/* only consider constraint if enabled */
if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) {
continue;
}
if (con->enforce == 0.0f) {
continue;
}
/* only use it if it's tagged for this purpose (and the right type) */
if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
bLocLimitConstraint *data = (bLocLimitConstraint *)con->data;
if ((data->flag2 & LIMIT_TRANSFORM) == 0) {
continue;
}
cti = ctiLoc;
}
else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
bDistLimitConstraint *data = (bDistLimitConstraint *)con->data;
if ((data->flag & LIMITDIST_TRANSFORM) == 0) {
continue;
}
cti = ctiDist;
}
if (cti) {
/* do space conversions */
if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
/* just multiply by td->mtx (this should be ok) */
mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
}
else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
/* skip... incompatible spacetype */
continue;
}
/* get constraint targets if needed */
BKE_constraint_targets_for_solving_get(t->depsgraph, con, &cob, &targets, ctime);
/* do constraint */
cti->evaluate_constraint(con, &cob, &targets);
/* convert spaces again */
if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
/* just multiply by td->smtx (this should be ok) */
mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
}
/* free targets list */
BLI_freelistN(&targets);
}
}
/* copy results from cob->matrix */
copy_v3_v3(td->loc, cob.matrix[3]);
}
}
static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
{
/* Make a temporary bConstraintOb for use by limit constraints
* - they only care that cob->matrix is correctly set ;-)
* - current space should be local
*/
memset(cob, 0, sizeof(bConstraintOb));
if (td->ext) {
if (td->ext->rotOrder == ROT_MODE_QUAT) {
/* quats */
/* objects and bones do normalization first too, otherwise
* we don't necessarily end up with a rotation matrix, and
* then conversion back to quat gives a different result */
float quat[4];
normalize_qt_qt(quat, td->ext->quat);
quat_to_mat4(cob->matrix, quat);
}
else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
/* axis angle */
axis_angle_to_mat4(cob->matrix, td->ext->rotAxis, *td->ext->rotAngle);
}
else {
/* eulers */
eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
}
}
}
static void constraintRotLim(const TransInfo *UNUSED(t), TransData *td)
{
if (td->con) {
const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_ROTLIMIT);
bConstraintOb cob;
bConstraint *con;
bool do_limit = false;
/* Evaluate valid constraints */
for (con = td->con; con; con = con->next) {
/* only consider constraint if enabled */
if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) {
continue;
}
if (con->enforce == 0.0f) {
continue;
}
/* we're only interested in Limit-Rotation constraints */
if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
bRotLimitConstraint *data = (bRotLimitConstraint *)con->data;
/* only use it if it's tagged for this purpose */
if ((data->flag2 & LIMIT_TRANSFORM) == 0) {
continue;
}
/* skip incompatible spacetypes */
if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL)) {
continue;
}
/* only do conversion if necessary, to preserve quats and eulers */
if (do_limit == false) {
constraintob_from_transdata(&cob, td);
do_limit = true;
}
/* do space conversions */
if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
/* just multiply by td->mtx (this should be ok) */
mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
}
/* do constraint */
cti->evaluate_constraint(con, &cob, NULL);
/* convert spaces again */
if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
/* just multiply by td->smtx (this should be ok) */
mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
}
}
}
if (do_limit) {
/* copy results from cob->matrix */
if (td->ext->rotOrder == ROT_MODE_QUAT) {
/* quats */
mat4_to_quat(td->ext->quat, cob.matrix);
}
else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
/* axis angle */
mat4_to_axis_angle(td->ext->rotAxis, td->ext->rotAngle, cob.matrix);
}
else {
/* eulers */
mat4_to_eulO(td->ext->rot, td->ext->rotOrder, cob.matrix);
}
}
}
}
void constraintSizeLim(const TransInfo *t, TransData *td)
{
if (td->con && td->ext) {
const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_SIZELIMIT);
bConstraintOb cob = {NULL};
bConstraint *con;
float size_sign[3], size_abs[3];
int i;
/* Make a temporary bConstraintOb for using these limit constraints
* - they only care that cob->matrix is correctly set ;-)
* - current space should be local
*/
if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
/* scale val and reset size */
return; /* TODO: fix this case */
}
/* Reset val if SINGLESIZE but using a constraint */
if (td->flag & TD_SINGLESIZE) {
return;
}
/* separate out sign to apply back later */
for (i = 0; i < 3; i++) {
size_sign[i] = signf(td->ext->size[i]);
size_abs[i] = fabsf(td->ext->size[i]);
}
size_to_mat4(cob.matrix, size_abs);
/* Evaluate valid constraints */
for (con = td->con; con; con = con->next) {
/* only consider constraint if enabled */
if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) {
continue;
}
if (con->enforce == 0.0f) {
continue;
}
/* we're only interested in Limit-Scale constraints */
if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
bSizeLimitConstraint *data = con->data;
/* only use it if it's tagged for this purpose */
if ((data->flag2 & LIMIT_TRANSFORM) == 0) {
continue;
}
/* do space conversions */
if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
/* just multiply by td->mtx (this should be ok) */
mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
}
else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
/* skip... incompatible spacetype */
continue;
}
/* do constraint */
cti->evaluate_constraint(con, &cob, NULL);
/* convert spaces again */
if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
/* just multiply by td->smtx (this should be ok) */
mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
}
}
}
/* copy results from cob->matrix */
if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
/* scale val and reset size */
return; /* TODO: fix this case. */
}
/* Reset val if SINGLESIZE but using a constraint */
if (td->flag & TD_SINGLESIZE) {
return;
}
/* Extract scale from matrix and apply back sign. */
mat4_to_size(td->ext->size, cob.matrix);
mul_v3_v3(td->ext->size, size_sign);
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Transform (Rotation Utils)
* \{ */
void headerRotation(TransInfo *t, char *str, const int str_size, float final)
{
size_t ofs = 0;
if (hasNumInput(&t->num)) {
char c[NUM_STR_REP_LEN];
outputNumInput(&(t->num), c, &t->scene->unit);
ofs += BLI_snprintf_rlen(
str + ofs, str_size - ofs, TIP_("Rotation: %s %s %s"), &c[0], t->con.text, t->proptext);
}
else {
ofs += BLI_snprintf_rlen(str + ofs,
str_size - ofs,
TIP_("Rotation: %.2f%s %s"),
RAD2DEGF(final),
t->con.text,
t->proptext);
}
if (t->flag & T_PROP_EDIT_ALL) {
ofs += BLI_snprintf_rlen(
str + ofs, str_size - ofs, TIP_(" Proportional size: %.2f"), t->prop_size);
}
}
void ElementRotation_ex(const TransInfo *t,
const TransDataContainer *tc,
TransData *td,
const float mat[3][3],
const float *center)
{
float vec[3], totmat[3][3], smat[3][3];
float eul[3], fmat[3][3], quat[4];
if (t->flag & T_POINTS) {
mul_m3_m3m3(totmat, mat, td->mtx);
mul_m3_m3m3(smat, td->smtx, totmat);
/* Apply gpencil falloff. */
if (t->options & CTX_GPENCIL_STROKES) {
bGPDstroke *gps = (bGPDstroke *)td->extra;
if (gps->runtime.multi_frame_falloff != 1.0f) {
float ident_mat[3][3];
unit_m3(ident_mat);
interp_m3_m3m3(smat, ident_mat, smat, gps->runtime.multi_frame_falloff);
}
}
sub_v3_v3v3(vec, td->iloc, center);
mul_m3_v3(smat, vec);
add_v3_v3v3(td->loc, vec, center);
sub_v3_v3v3(vec, td->loc, td->iloc);
protectedTransBits(td->protectflag, vec);
add_v3_v3v3(td->loc, td->iloc, vec);
if (td->flag & TD_USEQUAT) {
mul_m3_series(fmat, td->smtx, mat, td->mtx);
mat3_to_quat(quat, fmat); /* Actual transform */
if (td->ext->quat) {
mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
/* is there a reason not to have this here? -jahka */
protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
}
}
}
/**
* HACK WARNING
*
* This is some VERY ugly special case to deal with pose mode.
*
* The problem is that mtx and smtx include each bone orientation.
*
* That is needed to rotate each bone properly, HOWEVER, to calculate
* the translation component, we only need the actual armature object's
* matrix (and inverse). That is not all though. Once the proper translation
* has been computed, it has to be converted back into the bone's space.
*/
else if (t->options & CTX_POSE_BONE) {
/* Extract and invert armature object matrix */
if ((td->flag & TD_NO_LOC) == 0) {
sub_v3_v3v3(vec, td->center, center);
mul_m3_v3(tc->mat3, vec); /* To Global space. */
mul_m3_v3(mat, vec); /* Applying rotation. */
mul_m3_v3(tc->imat3, vec); /* To Local space. */
add_v3_v3(vec, center);
/* vec now is the location where the object has to be */
sub_v3_v3v3(vec, vec, td->center); /* Translation needed from the initial location */
/* special exception, see TD_PBONE_LOCAL_MTX definition comments */
if (td->flag & TD_PBONE_LOCAL_MTX_P) {
/* do nothing */
}
else if (td->flag & TD_PBONE_LOCAL_MTX_C) {
mul_m3_v3(tc->mat3, vec); /* To Global space. */
mul_m3_v3(td->ext->l_smtx, vec); /* To Pose space (Local Location). */
}
else {
mul_m3_v3(tc->mat3, vec); /* To Global space. */
mul_m3_v3(td->smtx, vec); /* To Pose space. */
}
protectedTransBits(td->protectflag, vec);
add_v3_v3v3(td->loc, td->iloc, vec);
constraintTransLim(t, td);
}
/* rotation */
/* MORE HACK: as in some cases the matrix to apply location and rot/scale is not the same,
* and ElementRotation() might be called in Translation context (with align snapping),
* we need to be sure to actually use the *rotation* matrix here...
* So no other way than storing it in some dedicated members of td->ext! */
if ((t->flag & T_V3D_ALIGN) == 0) { /* align mode doesn't rotate objects itself */
/* euler or quaternion/axis-angle? */
if (td->ext->rotOrder == ROT_MODE_QUAT) {
mul_m3_series(fmat, td->ext->r_smtx, mat, td->ext->r_mtx);
mat3_to_quat(quat, fmat); /* Actual transform */
mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
/* this function works on end result */
protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
}
else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
/* calculate effect based on quats */
float iquat[4], tquat[4];
axis_angle_to_quat(iquat, td->ext->irotAxis, td->ext->irotAngle);
mul_m3_series(fmat, td->ext->r_smtx, mat, td->ext->r_mtx);
mat3_to_quat(quat, fmat); /* Actual transform */
mul_qt_qtqt(tquat, quat, iquat);
quat_to_axis_angle(td->ext->rotAxis, td->ext->rotAngle, tquat);
/* this function works on end result */
protectedAxisAngleBits(td->protectflag,
td->ext->rotAxis,
td->ext->rotAngle,
td->ext->irotAxis,
td->ext->irotAngle);
}
else {
float eulmat[3][3];
mul_m3_m3m3(totmat, mat, td->ext->r_mtx);
mul_m3_m3m3(smat, td->ext->r_smtx, totmat);
/* Calculate the total rotation in eulers. */
copy_v3_v3(eul, td->ext->irot);
eulO_to_mat3(eulmat, eul, td->ext->rotOrder);
/* mat = transform, obmat = bone rotation */
mul_m3_m3m3(fmat, smat, eulmat);
mat3_to_compatible_eulO(eul, td->ext->rot, td->ext->rotOrder, fmat);
/* and apply (to end result only) */
protectedRotateBits(td->protectflag, eul, td->ext->irot);
copy_v3_v3(td->ext->rot, eul);
}
constraintRotLim(t, td);
}
}
else {
if ((td->flag & TD_NO_LOC) == 0) {
/* translation */
sub_v3_v3v3(vec, td->center, center);
mul_m3_v3(mat, vec);
add_v3_v3(vec, center);
/* vec now is the location where the object has to be */
sub_v3_v3(vec, td->center);
mul_m3_v3(td->smtx, vec);
protectedTransBits(td->protectflag, vec);
add_v3_v3v3(td->loc, td->iloc, vec);
}
constraintTransLim(t, td);
/* rotation */
if ((t->flag & T_V3D_ALIGN) == 0) { /* Align mode doesn't rotate objects itself. */
/* euler or quaternion? */
if ((td->ext->rotOrder == ROT_MODE_QUAT) || (td->flag & TD_USEQUAT)) {
/* can be called for texture space translate for example, then opt out */
if (td->ext->quat) {
mul_m3_series(fmat, td->smtx, mat, td->mtx);
if (!is_zero_v3(td->ext->dquat)) {
/* Correct for delta quat */
float tmp_mat[3][3];
quat_to_mat3(tmp_mat, td->ext->dquat);
mul_m3_m3m3(fmat, fmat, tmp_mat);
}
mat3_to_quat(quat, fmat); /* Actual transform */
if (!is_zero_v4(td->ext->dquat)) {
/* Correct back for delta quat. */
float idquat[4];
invert_qt_qt_normalized(idquat, td->ext->dquat);
mul_qt_qtqt(quat, idquat, quat);
}
mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
/* this function works on end result */
protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
}
}
else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
/* calculate effect based on quats */
float iquat[4], tquat[4];
axis_angle_to_quat(iquat, td->ext->irotAxis, td->ext->irotAngle);
mul_m3_series(fmat, td->smtx, mat, td->mtx);
mat3_to_quat(quat, fmat); /* Actual transform */
mul_qt_qtqt(tquat, quat, iquat);
quat_to_axis_angle(td->ext->rotAxis, td->ext->rotAngle, tquat);
/* this function works on end result */
protectedAxisAngleBits(td->protectflag,
td->ext->rotAxis,
td->ext->rotAngle,
td->ext->irotAxis,
td->ext->irotAngle);
}
else {
/* Calculate the total rotation in eulers. */
float obmat[3][3];
mul_m3_m3m3(totmat, mat, td->mtx);
mul_m3_m3m3(smat, td->smtx, totmat);
if (!is_zero_v3(td->ext->drot)) {
/* Correct for delta rot */
add_eul_euleul(eul, td->ext->irot, td->ext->drot, td->ext->rotOrder);
}
else {
copy_v3_v3(eul, td->ext->irot);
}
eulO_to_mat3(obmat, eul, td->ext->rotOrder);
mul_m3_m3m3(fmat, smat, obmat);
mat3_to_compatible_eulO(eul, td->ext->rot, td->ext->rotOrder, fmat);
if (!is_zero_v3(td->ext->drot)) {
/* Correct back for delta rot. */
sub_eul_euleul(eul, eul, td->ext->drot, td->ext->rotOrder);
}
/* and apply */
protectedRotateBits(td->protectflag, eul, td->ext->irot);
copy_v3_v3(td->ext->rot, eul);
}
constraintRotLim(t, td);
}
}
}
void ElementRotation(const TransInfo *t,
const TransDataContainer *tc,
TransData *td,
const float mat[3][3],
const short around)
{
const float *center;
/* local constraint shouldn't alter center */
if (transdata_check_local_center(t, around)) {
center = td->center;
}
else {
center = tc->center_local;
}
ElementRotation_ex(t, tc, td, mat, center);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Transform (Resize Utils)
* \{ */
void headerResize(TransInfo *t, const float vec[3], char *str, const int str_size)
{
char tvec[NUM_STR_REP_LEN * 3];
size_t ofs = 0;
if (hasNumInput(&t->num)) {
outputNumInput(&(t->num), tvec, &t->scene->unit);
}
else {
BLI_snprintf(&tvec[0], NUM_STR_REP_LEN, "%.4f", vec[0]);
BLI_snprintf(&tvec[NUM_STR_REP_LEN], NUM_STR_REP_LEN, "%.4f", vec[1]);
BLI_snprintf(&tvec[NUM_STR_REP_LEN * 2], NUM_STR_REP_LEN, "%.4f", vec[2]);
}
if (t->con.mode & CON_APPLY) {
switch (t->num.idx_max) {
case 0:
ofs += BLI_snprintf_rlen(
str + ofs, str_size - ofs, TIP_("Scale: %s%s %s"), &tvec[0], t->con.text, t->proptext);
break;
case 1:
ofs += BLI_snprintf_rlen(str + ofs,
str_size - ofs,
TIP_("Scale: %s : %s%s %s"),
&tvec[0],
&tvec[NUM_STR_REP_LEN],
t->con.text,
t->proptext);
break;
case 2:
ofs += BLI_snprintf_rlen(str + ofs,
str_size - ofs,
TIP_("Scale: %s : %s : %s%s %s"),
&tvec[0],
&tvec[NUM_STR_REP_LEN],
&tvec[NUM_STR_REP_LEN * 2],
t->con.text,
t->proptext);
break;
}
}
else {
if (t->flag & T_2D_EDIT) {
ofs += BLI_snprintf_rlen(str + ofs,
str_size - ofs,
TIP_("Scale X: %s Y: %s%s %s"),
&tvec[0],
&tvec[NUM_STR_REP_LEN],
t->con.text,
t->proptext);
}
else {
ofs += BLI_snprintf_rlen(str + ofs,
str_size - ofs,
TIP_("Scale X: %s Y: %s Z: %s%s %s"),
&tvec[0],
&tvec[NUM_STR_REP_LEN],
&tvec[NUM_STR_REP_LEN * 2],
t->con.text,
t->proptext);
}
}
if (t->flag & T_PROP_EDIT_ALL) {
ofs += BLI_snprintf_rlen(
str + ofs, str_size - ofs, TIP_(" Proportional size: %.2f"), t->prop_size);
}
}
/**
* \a smat is reference matrix only.
*
* \note this is a tricky area, before making changes see: T29633, T42444
*/
static void TransMat3ToSize(const float mat[3][3], const float smat[3][3], float size[3])
{
float rmat[3][3];
mat3_to_rot_size(rmat, size, mat);
/* First tried with dot-product... but the sign flip is crucial. */
if (dot_v3v3(rmat[0], smat[0]) < 0.0f) {
size[0] = -size[0];
}
if (dot_v3v3(rmat[1], smat[1]) < 0.0f) {
size[1] = -size[1];
}
if (dot_v3v3(rmat[2], smat[2]) < 0.0f) {
size[2] = -size[2];
}
}
void ElementResize(const TransInfo *t,
const TransDataContainer *tc,
TransData *td,
const float mat[3][3])
{
float tmat[3][3], smat[3][3], center[3];
float vec[3];
if (t->flag & T_EDIT) {
mul_m3_m3m3(smat, mat, td->mtx);
mul_m3_m3m3(tmat, td->smtx, smat);
}
else {
copy_m3_m3(tmat, mat);
}
if (t->con.applySize) {
t->con.applySize(t, tc, td, tmat);
}
/* local constraint shouldn't alter center */
if (transdata_check_local_center(t, t->around)) {
copy_v3_v3(center, td->center);
}
else if (t->options & CTX_MOVIECLIP) {
if (td->flag & TD_INDIVIDUAL_SCALE) {
copy_v3_v3(center, td->center);
}
else {
copy_v3_v3(center, tc->center_local);
}
}
else {
copy_v3_v3(center, tc->center_local);
}
/* Size checked needed since the 3D cursor only uses rotation fields. */
if (td->ext && td->ext->size) {
float fsize[3];
if (ELEM(t->data_type, TC_SCULPT, TC_OBJECT, TC_OBJECT_TEXSPACE, TC_POSE)) {
float obsizemat[3][3];
/* Reorient the size mat to fit the oriented object. */
mul_m3_m3m3(obsizemat, tmat, td->axismtx);
// print_m3("obsizemat", obsizemat);
TransMat3ToSize(obsizemat, td->axismtx, fsize);
// print_v3("fsize", fsize);
}
else {
mat3_to_size(fsize, tmat);
}
protectedSizeBits(td->protectflag, fsize);
if ((t->flag & T_V3D_ALIGN) == 0) { /* align mode doesn't resize objects itself */
if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
/* scale val and reset size */
*td->val = td->ival * (1 + (fsize[0] - 1) * td->factor);
td->ext->size[0] = td->ext->isize[0];
td->ext->size[1] = td->ext->isize[1];
td->ext->size[2] = td->ext->isize[2];
}
else {
/* Reset val if SINGLESIZE but using a constraint */
if (td->flag & TD_SINGLESIZE) {
*td->val = td->ival;
}
td->ext->size[0] = td->ext->isize[0] * (1 + (fsize[0] - 1) * td->factor);
td->ext->size[1] = td->ext->isize[1] * (1 + (fsize[1] - 1) * td->factor);
td->ext->size[2] = td->ext->isize[2] * (1 + (fsize[2] - 1) * td->factor);
}
}
constraintSizeLim(t, td);
}
/* For individual element center, Editmode need to use iloc */
if (t->flag & T_POINTS) {
sub_v3_v3v3(vec, td->iloc, center);
}
else {
sub_v3_v3v3(vec, td->center, center);
}
mul_m3_v3(tmat, vec);
add_v3_v3(vec, center);
if (t->flag & T_POINTS) {
sub_v3_v3(vec, td->iloc);
}
else {
sub_v3_v3(vec, td->center);
}
/* Grease pencil falloff.
*
* FIXME: This is bad on multiple levels!
*
* - #applyNumInput is not intended to be run for every element,
* this writes back into the number input in a way that doesn't make sense to run many times.
*
* - Writing into #TransInfo should be avoided since it means order of operations
* may impact the result and isn't thread-safe.
*
* Operating on copies as a temporary solution.
*/
if (t->options & CTX_GPENCIL_STROKES) {
bGPDstroke *gps = (bGPDstroke *)td->extra;
mul_v3_fl(vec, td->factor * gps->runtime.multi_frame_falloff);
/* Scale stroke thickness. */
if (td->val) {
NumInput num_evil = t->num;
float values_final_evil[4];
copy_v4_v4(values_final_evil, t->values_final);
transform_snap_increment(t, values_final_evil);
applyNumInput(&num_evil, values_final_evil);
float ratio = values_final_evil[0];
*td->val = td->ival * fabs(ratio) * gps->runtime.multi_frame_falloff;
CLAMP_MIN(*td->val, 0.001f);
}
}
else {
mul_v3_fl(vec, td->factor);
}
if (t->options & (CTX_OBJECT | CTX_POSE_BONE)) {
if (t->options & CTX_POSE_BONE) {
/* Without this, the resulting location of scaled bones aren't correct,
* especially noticeable scaling root or disconnected bones around the cursor, see T92515. */
mul_mat3_m4_v3(tc->poseobj->obmat, vec);
}
mul_m3_v3(td->smtx, vec);
}
protectedTransBits(td->protectflag, vec);
if (td->loc) {
add_v3_v3v3(td->loc, td->iloc, vec);
}
constraintTransLim(t, td);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Transform Mode Initialization
* \{ */
void transform_mode_init(TransInfo *t, wmOperator *op, const int mode)
{
t->mode = mode;
switch (mode) {
case TFM_TRANSLATION:
initTranslation(t);
break;
case TFM_ROTATION:
initRotation(t);
break;
case TFM_RESIZE: {
float mouse_dir_constraint[3];
if (op) {
PropertyRNA *prop = RNA_struct_find_property(op->ptr, "mouse_dir_constraint");
if (prop) {
RNA_property_float_get_array(op->ptr, prop, mouse_dir_constraint);
}
else {
/* Resize is expected to have this property. */
BLI_assert(!STREQ(op->idname, "TRANSFORM_OT_resize"));
}
}
else {
zero_v3(mouse_dir_constraint);
}
initResize(t, mouse_dir_constraint);
break;
}
case TFM_SKIN_RESIZE:
initSkinResize(t);
break;
case TFM_TOSPHERE:
initToSphere(t);
break;
case TFM_SHEAR:
initShear(t);
break;
case TFM_BEND:
initBend(t);
break;
case TFM_SHRINKFATTEN:
initShrinkFatten(t);
break;
case TFM_TILT:
initTilt(t);
break;
case TFM_CURVE_SHRINKFATTEN:
initCurveShrinkFatten(t);
break;
case TFM_MASK_SHRINKFATTEN:
initMaskShrinkFatten(t);
break;
case TFM_GPENCIL_SHRINKFATTEN:
initGPShrinkFatten(t);
break;
case TFM_TRACKBALL:
initTrackball(t);
break;
case TFM_PUSHPULL:
initPushPull(t);
break;
case TFM_EDGE_CREASE:
initEgdeCrease(t);
break;
case TFM_VERT_CREASE:
initVertCrease(t);
break;
case TFM_BONESIZE:
initBoneSize(t);
break;
case TFM_BONE_ENVELOPE:
case TFM_BONE_ENVELOPE_DIST:
initBoneEnvelope(t);
break;
case TFM_EDGE_SLIDE:
case TFM_VERT_SLIDE: {
const bool use_even = (op ? RNA_boolean_get(op->ptr, "use_even") : false);
const bool flipped = (op ? RNA_boolean_get(op->ptr, "flipped") : false);
const bool use_clamp = (op ? RNA_boolean_get(op->ptr, "use_clamp") : true);
if (mode == TFM_EDGE_SLIDE) {
const bool use_double_side = (op ? !RNA_boolean_get(op->ptr, "single_side") : true);
initEdgeSlide_ex(t, use_double_side, use_even, flipped, use_clamp);
}
else {
initVertSlide_ex(t, use_even, flipped, use_clamp);
}
break;
}
case TFM_BONE_ROLL:
initBoneRoll(t);
break;
case TFM_TIME_TRANSLATE:
initTimeTranslate(t);
break;
case TFM_TIME_SLIDE:
initTimeSlide(t);
break;
case TFM_TIME_SCALE:
initTimeScale(t);
break;
case TFM_TIME_DUPLICATE:
/* same as TFM_TIME_EXTEND, but we need the mode info for later
* so that duplicate-culling will work properly
*/
if (ELEM(t->spacetype, SPACE_GRAPH, SPACE_NLA)) {
initTranslation(t);
}
else {
initTimeTranslate(t);
}
break;
case TFM_TIME_EXTEND:
/* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
* Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
* (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
* depending on which editor this was called from
*/
if (ELEM(t->spacetype, SPACE_GRAPH, SPACE_NLA)) {
initTranslation(t);
}
else {
initTimeTranslate(t);
}
break;
case TFM_BAKE_TIME:
initBakeTime(t);
break;
case TFM_MIRROR:
initMirror(t);
break;
case TFM_BWEIGHT:
initBevelWeight(t);
break;
case TFM_ALIGN:
initAlign(t);
break;
case TFM_SEQ_SLIDE:
initSeqSlide(t);
break;
case TFM_NORMAL_ROTATION:
initNormalRotation(t);
break;
case TFM_GPENCIL_OPACITY:
initGPOpacity(t);
break;
}
if (t->data_type == TC_MESH_VERTS) {
/* Init Custom Data correction.
* Ideally this should be called when creating the TransData. */
transform_convert_mesh_customdatacorrect_init(t);
}
/* TODO(germano): Some of these operations change the `t->mode`.
* This can be bad for Redo. */
// BLI_assert(t->mode == mode);
}
void transform_mode_default_modal_orientation_set(TransInfo *t, int type)
{
/* Currently only these types are supported. */
BLI_assert(ELEM(type, V3D_ORIENT_GLOBAL, V3D_ORIENT_VIEW));
if (t->is_orient_default_overwrite) {
return;
}
if (!(t->flag & T_MODAL)) {
return;
}
if (t->orient[O_DEFAULT].type == type) {
return;
}
View3D *v3d = NULL;
RegionView3D *rv3d = NULL;
if ((type == V3D_ORIENT_VIEW) && (t->spacetype == SPACE_VIEW3D) && t->region &&
(t->region->regiontype == RGN_TYPE_WINDOW)) {
v3d = t->view;
rv3d = t->region->regiondata;
}
t->orient[O_DEFAULT].type = ED_transform_calc_orientation_from_type_ex(
t->scene,
t->view_layer,
v3d,
rv3d,
NULL,
NULL,
type,
V3D_AROUND_CENTER_BOUNDS,
t->orient[O_DEFAULT].matrix);
if (t->orient_curr == O_DEFAULT) {
/* Update Orientation. */
transform_orientations_current_set(t, O_DEFAULT);
}
}
/** \} */
|