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
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/transform/transform_snap_object.c
* \ingroup edtransform
*/
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <stdio.h>
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "BLI_kdopbvh.h"
#include "BLI_memarena.h"
#include "BLI_ghash.h"
#include "BLI_linklist.h"
#include "BLI_listbase.h"
#include "BLI_utildefines.h"
#include "DNA_armature_types.h"
#include "DNA_curve_types.h"
#include "DNA_scene_types.h"
#include "DNA_object_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_screen_types.h"
#include "DNA_view3d_types.h"
#include "BKE_DerivedMesh.h"
#include "BKE_object.h"
#include "BKE_anim.h" /* for duplis */
#include "BKE_editmesh.h"
#include "BKE_main.h"
#include "BKE_tracking.h"
#include "ED_transform.h"
#include "ED_transform_snap_object_context.h"
#include "ED_view3d.h"
#include "ED_armature.h"
#include "transform.h"
typedef struct SnapObjectData {
enum {
SNAP_MESH = 1,
SNAP_EDIT_MESH,
} type;
} SnapObjectData;
typedef struct SnapObjectData_Mesh {
SnapObjectData sd;
BVHTreeFromMesh *bvh_trees[2];
} SnapObjectData_Mesh;
typedef struct SnapObjectData_EditMesh {
SnapObjectData sd;
BVHTreeFromEditMesh *bvh_trees[2];
} SnapObjectData_EditMesh;
struct SnapObjectContext {
Main *bmain;
Scene *scene;
int flag;
/* Optional: when performing screen-space projection.
* otherwise this doesn't take viewport into account. */
bool use_v3d;
struct {
struct View3D *v3d;
struct ARegion *ar;
} v3d_data;
/* Object -> SnapObjectData map */
struct {
GHash *object_map;
MemArena *mem_arena;
} cache;
/* Filter data, returns true to check this value */
struct {
struct {
bool (*test_vert_fn)(BMVert *, void *user_data);
bool (*test_edge_fn)(BMEdge *, void *user_data);
bool (*test_face_fn)(BMFace *, void *user_data);
void *user_data;
} edit_mesh;
} callbacks;
};
static int dm_looptri_to_poly_index(DerivedMesh *dm, const MLoopTri *lt);
/* -------------------------------------------------------------------- */
/** \name Support for storing all depths, not just the first (raycast 'all')
*
* This uses a list of #SnapObjectHitDepth structs.
*
* \{ */
/* Store all ray-hits */
struct RayCastAll_Data {
void *bvhdata;
/* internal vars for adding depths */
BVHTree_RayCastCallback raycast_callback;
const float(*obmat)[4];
const float(*timat)[3];
float len_diff;
float local_scale;
Object *ob;
unsigned int ob_uuid;
/* DerivedMesh only */
DerivedMesh *dm;
const struct MLoopTri *dm_looptri;
/* output data */
ListBase *hit_list;
bool retval;
};
static struct SnapObjectHitDepth *hit_depth_create(
const float depth, const float co[3], const float no[3], int index,
Object *ob, const float obmat[4][4], unsigned int ob_uuid)
{
struct SnapObjectHitDepth *hit = MEM_mallocN(sizeof(*hit), __func__);
hit->depth = depth;
copy_v3_v3(hit->co, co);
copy_v3_v3(hit->no, no);
hit->index = index;
hit->ob = ob;
copy_m4_m4(hit->obmat, (float(*)[4])obmat);
hit->ob_uuid = ob_uuid;
return hit;
}
static int hit_depth_cmp(const void *arg1, const void *arg2)
{
const struct SnapObjectHitDepth *h1 = arg1;
const struct SnapObjectHitDepth *h2 = arg2;
int val = 0;
if (h1->depth < h2->depth) {
val = -1;
}
else if (h1->depth > h2->depth) {
val = 1;
}
return val;
}
static void raycast_all_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
{
struct RayCastAll_Data *data = userdata;
data->raycast_callback(data->bvhdata, index, ray, hit);
if (hit->index != -1) {
/* get all values in worldspace */
float location[3], normal[3];
float depth;
/* worldspace location */
mul_v3_m4v3(location, (float(*)[4])data->obmat, hit->co);
depth = (hit->dist + data->len_diff) / data->local_scale;
/* worldspace normal */
copy_v3_v3(normal, hit->no);
mul_m3_v3((float(*)[3])data->timat, normal);
normalize_v3(normal);
/* currently unused, and causes issues when looptri's havn't been calculated.
* since theres some overhead in ensuring this data is valid, it may need to be optional. */
#if 0
if (data->dm) {
hit->index = dm_looptri_to_poly_index(data->dm, &data->dm_looptri[hit->index]);
}
#endif
struct SnapObjectHitDepth *hit_item = hit_depth_create(
depth, location, normal, hit->index,
data->ob, data->obmat, data->ob_uuid);
BLI_addtail(data->hit_list, hit_item);
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Internal Object Snapping API
* \{ */
static bool snapEdge(
ARegion *ar, const float v1co[3], const short v1no[3], const float v2co[3], const short v2no[3],
float obmat[4][4], float timat[3][3], const float mval_fl[2], float *dist_px,
const float ray_start[3], const float ray_start_local[3], const float ray_normal_local[3], float *ray_depth,
float r_loc[3], float r_no[3])
{
float intersect[3] = {0, 0, 0}, ray_end[3], dvec[3];
int result;
bool retval = false;
copy_v3_v3(ray_end, ray_normal_local);
mul_v3_fl(ray_end, 2000);
add_v3_v3v3(ray_end, ray_start_local, ray_end);
/* dvec used but we don't care about result */
result = isect_line_line_v3(v1co, v2co, ray_start_local, ray_end, intersect, dvec);
if (result) {
float edge_loc[3], vec[3];
float mul;
/* check for behind ray_start */
sub_v3_v3v3(dvec, intersect, ray_start_local);
sub_v3_v3v3(edge_loc, v1co, v2co);
sub_v3_v3v3(vec, intersect, v2co);
mul = dot_v3v3(vec, edge_loc) / dot_v3v3(edge_loc, edge_loc);
if (mul > 1) {
mul = 1;
copy_v3_v3(intersect, v1co);
}
else if (mul < 0) {
mul = 0;
copy_v3_v3(intersect, v2co);
}
if (dot_v3v3(ray_normal_local, dvec) > 0) {
float location[3];
float new_depth;
float screen_loc[2];
float new_dist;
copy_v3_v3(location, intersect);
mul_m4_v3(obmat, location);
new_depth = len_v3v3(location, ray_start);
if (ED_view3d_project_float_global(ar, location, screen_loc, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
new_dist = len_manhattan_v2v2(mval_fl, screen_loc);
}
else {
new_dist = TRANSFORM_DIST_MAX_PX;
}
/* 10% threshold if edge is closer but a bit further
* this takes care of series of connected edges a bit slanted w.r.t the viewport
* otherwise, it would stick to the verts of the closest edge and not slide along merrily
* */
if (new_dist <= *dist_px && new_depth < *ray_depth * 1.001f) {
float n1[3], n2[3];
*ray_depth = new_depth;
retval = true;
sub_v3_v3v3(edge_loc, v1co, v2co);
sub_v3_v3v3(vec, intersect, v2co);
mul = dot_v3v3(vec, edge_loc) / dot_v3v3(edge_loc, edge_loc);
if (r_no) {
normal_short_to_float_v3(n1, v1no);
normal_short_to_float_v3(n2, v2no);
interp_v3_v3v3(r_no, n2, n1, mul);
mul_m3_v3(timat, r_no);
normalize_v3(r_no);
}
copy_v3_v3(r_loc, location);
*dist_px = new_dist;
}
}
}
return retval;
}
static bool snapVertex(
ARegion *ar, const float vco[3], const float vno[3],
float obmat[4][4], float timat[3][3], const float mval_fl[2], float *dist_px,
const float ray_start[3], const float ray_start_local[3], const float ray_normal_local[3], float *ray_depth,
float r_loc[3], float r_no[3])
{
bool retval = false;
float dvec[3];
sub_v3_v3v3(dvec, vco, ray_start_local);
if (dot_v3v3(ray_normal_local, dvec) > 0) {
float location[3];
float new_depth;
float screen_loc[2];
float new_dist;
copy_v3_v3(location, vco);
mul_m4_v3(obmat, location);
new_depth = len_v3v3(location, ray_start);
if (ED_view3d_project_float_global(ar, location, screen_loc, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
new_dist = len_manhattan_v2v2(mval_fl, screen_loc);
}
else {
new_dist = TRANSFORM_DIST_MAX_PX;
}
if (new_dist <= *dist_px && new_depth < *ray_depth) {
*ray_depth = new_depth;
retval = true;
copy_v3_v3(r_loc, location);
if (r_no) {
copy_v3_v3(r_no, vno);
mul_m3_v3(timat, r_no);
normalize_v3(r_no);
}
*dist_px = new_dist;
}
}
return retval;
}
static bool snapArmature(
ARegion *ar, Object *ob, bArmature *arm, float obmat[4][4],
const float mval[2], float *dist_px, const short snap_to,
const float ray_start[3], const float ray_normal[3], float *ray_depth,
float r_loc[3], float *UNUSED(r_no))
{
float imat[4][4];
float ray_start_local[3], ray_normal_local[3];
bool retval = false;
invert_m4_m4(imat, obmat);
mul_v3_m4v3(ray_start_local, imat, ray_start);
mul_v3_mat3_m4v3(ray_normal_local, imat, ray_normal);
if (arm->edbo) {
EditBone *eBone;
for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
if (eBone->layer & arm->layer) {
/* skip hidden or moving (selected) bones */
if ((eBone->flag & (BONE_HIDDEN_A | BONE_ROOTSEL | BONE_TIPSEL)) == 0) {
switch (snap_to) {
case SCE_SNAP_MODE_VERTEX:
retval |= snapVertex(
ar, eBone->head, NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
retval |= snapVertex(
ar, eBone->tail, NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
break;
case SCE_SNAP_MODE_EDGE:
retval |= snapEdge(
ar, eBone->head, NULL, eBone->tail, NULL,
obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local,
ray_depth, r_loc, NULL);
break;
}
}
}
}
}
else if (ob->pose && ob->pose->chanbase.first) {
bPoseChannel *pchan;
Bone *bone;
for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
bone = pchan->bone;
/* skip hidden bones */
if (bone && !(bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) {
const float *head_vec = pchan->pose_head;
const float *tail_vec = pchan->pose_tail;
switch (snap_to) {
case SCE_SNAP_MODE_VERTEX:
retval |= snapVertex(
ar, head_vec, NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local,
ray_depth, r_loc, NULL);
retval |= snapVertex(
ar, tail_vec, NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
break;
case SCE_SNAP_MODE_EDGE:
retval |= snapEdge(
ar, head_vec, NULL, tail_vec, NULL,
obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local,
ray_depth, r_loc, NULL);
break;
}
}
}
}
return retval;
}
static bool snapCurve(
ARegion *ar, Object *ob, Curve *cu, float obmat[4][4],
const float mval[2], float *dist_px, const short snap_to,
const float ray_start[3], const float ray_normal[3], float *ray_depth,
float r_loc[3], float *UNUSED(r_no))
{
float imat[4][4];
float ray_start_local[3], ray_normal_local[3];
bool retval = false;
int u;
Nurb *nu;
/* only vertex snapping mode (eg control points and handles) supported for now) */
if (snap_to != SCE_SNAP_MODE_VERTEX) {
return retval;
}
invert_m4_m4(imat, obmat);
copy_v3_v3(ray_start_local, ray_start);
copy_v3_v3(ray_normal_local, ray_normal);
mul_m4_v3(imat, ray_start_local);
mul_mat3_m4_v3(imat, ray_normal_local);
for (nu = (ob->mode == OB_MODE_EDIT ? cu->editnurb->nurbs.first : cu->nurb.first); nu; nu = nu->next) {
for (u = 0; u < nu->pntsu; u++) {
switch (snap_to) {
case SCE_SNAP_MODE_VERTEX:
{
if (ob->mode == OB_MODE_EDIT) {
if (nu->bezt) {
/* don't snap to selected (moving) or hidden */
if (nu->bezt[u].f2 & SELECT || nu->bezt[u].hide != 0) {
break;
}
retval |= snapVertex(
ar, nu->bezt[u].vec[1], NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
/* don't snap if handle is selected (moving), or if it is aligning to a moving handle */
if (!(nu->bezt[u].f1 & SELECT) &&
!(nu->bezt[u].h1 & HD_ALIGN && nu->bezt[u].f3 & SELECT))
{
retval |= snapVertex(
ar, nu->bezt[u].vec[0], NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
}
if (!(nu->bezt[u].f3 & SELECT) &&
!(nu->bezt[u].h2 & HD_ALIGN && nu->bezt[u].f1 & SELECT))
{
retval |= snapVertex(
ar, nu->bezt[u].vec[2], NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
}
}
else {
/* don't snap to selected (moving) or hidden */
if (nu->bp[u].f1 & SELECT || nu->bp[u].hide != 0) {
break;
}
retval |= snapVertex(
ar, nu->bp[u].vec, NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
}
}
else {
/* curve is not visible outside editmode if nurb length less than two */
if (nu->pntsu > 1) {
if (nu->bezt) {
retval |= snapVertex(
ar, nu->bezt[u].vec[1], NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
}
else {
retval |= snapVertex(
ar, nu->bp[u].vec, NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
}
}
}
break;
}
default:
break;
}
}
}
return retval;
}
/* may extend later (for now just snaps to empty center) */
static bool snapEmpty(
ARegion *ar, Object *ob, float obmat[4][4],
const float mval[2], float *dist_px, const short snap_to,
const float ray_start[3], const float ray_normal[3], float *ray_depth,
float r_loc[3], float *UNUSED(r_no))
{
float imat[4][4];
float ray_start_local[3], ray_normal_local[3];
bool retval = false;
if (ob->transflag & OB_DUPLI) {
return retval;
}
/* for now only vertex supported */
if (snap_to != SCE_SNAP_MODE_VERTEX) {
return retval;
}
invert_m4_m4(imat, obmat);
mul_v3_m4v3(ray_start_local, imat, ray_start);
mul_v3_mat3_m4v3(ray_normal_local, imat, ray_normal);
switch (snap_to) {
case SCE_SNAP_MODE_VERTEX:
{
const float zero_co[3] = {0.0f};
retval |= snapVertex(
ar, zero_co, NULL, obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
break;
}
default:
break;
}
return retval;
}
static bool snapCamera(
ARegion *ar, Scene *scene, Object *object, float obmat[4][4],
const float mval[2], float *dist_px, const short snap_to,
const float ray_start[3], const float ray_normal[3], float *ray_depth,
float r_loc[3], float *UNUSED(r_no))
{
float orig_camera_mat[4][4], orig_camera_imat[4][4], imat[4][4];
bool retval = false;
MovieClip *clip = BKE_object_movieclip_get(scene, object, false);
MovieTracking *tracking;
float ray_start_local[3], ray_normal_local[3];
if (clip == NULL) {
return retval;
}
if (object->transflag & OB_DUPLI) {
return retval;
}
tracking = &clip->tracking;
BKE_tracking_get_camera_object_matrix(scene, object, orig_camera_mat);
invert_m4_m4(orig_camera_imat, orig_camera_mat);
invert_m4_m4(imat, obmat);
switch (snap_to) {
case SCE_SNAP_MODE_VERTEX:
{
MovieTrackingObject *tracking_object;
for (tracking_object = tracking->objects.first;
tracking_object;
tracking_object = tracking_object->next)
{
ListBase *tracksbase = BKE_tracking_object_get_tracks(tracking, tracking_object);
MovieTrackingTrack *track;
float reconstructed_camera_mat[4][4],
reconstructed_camera_imat[4][4];
float (*vertex_obmat)[4];
copy_v3_v3(ray_start_local, ray_start);
copy_v3_v3(ray_normal_local, ray_normal);
if ((tracking_object->flag & TRACKING_OBJECT_CAMERA) == 0) {
BKE_tracking_camera_get_reconstructed_interpolate(tracking, tracking_object,
CFRA, reconstructed_camera_mat);
invert_m4_m4(reconstructed_camera_imat, reconstructed_camera_mat);
}
for (track = tracksbase->first; track; track = track->next) {
float bundle_pos[3];
if ((track->flag & TRACK_HAS_BUNDLE) == 0) {
continue;
}
copy_v3_v3(bundle_pos, track->bundle_pos);
if (tracking_object->flag & TRACKING_OBJECT_CAMERA) {
mul_m4_v3(orig_camera_imat, ray_start_local);
mul_mat3_m4_v3(orig_camera_imat, ray_normal_local);
vertex_obmat = orig_camera_mat;
}
else {
mul_m4_v3(reconstructed_camera_imat, bundle_pos);
mul_m4_v3(imat, ray_start_local);
mul_mat3_m4_v3(imat, ray_normal_local);
vertex_obmat = obmat;
}
retval |= snapVertex(
ar, bundle_pos, NULL, vertex_obmat, NULL, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, NULL);
}
}
break;
}
default:
break;
}
return retval;
}
static int dm_looptri_to_poly_index(DerivedMesh *dm, const MLoopTri *lt)
{
const int *index_mp_to_orig = dm->getPolyDataArray(dm, CD_ORIGINDEX);
return index_mp_to_orig ? index_mp_to_orig[lt->poly] : lt->poly;
}
static bool snapDerivedMesh(
SnapObjectContext *sctx,
Object *ob, DerivedMesh *dm, float obmat[4][4],
const float mval[2], float *dist_px, const short snap_to, bool do_bb,
const float ray_start[3], const float ray_normal[3], const float ray_origin[3],
float *ray_depth, unsigned int ob_index,
float r_loc[3], float r_no[3], int *r_index,
ListBase *r_hit_list)
{
ARegion *ar = sctx->v3d_data.ar;
bool retval = false;
if (snap_to == SCE_SNAP_MODE_FACE) {
if (dm->getNumPolys(dm) == 0) {
return retval;
}
}
if (snap_to == SCE_SNAP_MODE_EDGE) {
if (dm->getNumEdges(dm) == 0) {
return retval;
}
}
else {
if (dm->getNumVerts(dm) == 0) {
return retval;
}
}
{
const bool do_ray_start_correction = (
ELEM(snap_to, SCE_SNAP_MODE_FACE, SCE_SNAP_MODE_VERTEX) &&
(sctx->use_v3d && !((RegionView3D *)sctx->v3d_data.ar->regiondata)->is_persp));
bool need_ray_start_correction_init = do_ray_start_correction;
float imat[4][4];
float timat[3][3]; /* transpose inverse matrix for normals */
float ray_start_local[3], ray_normal_local[3];
float local_scale, local_depth, len_diff;
invert_m4_m4(imat, obmat);
transpose_m3_m4(timat, imat);
copy_v3_v3(ray_start_local, ray_start);
copy_v3_v3(ray_normal_local, ray_normal);
mul_m4_v3(imat, ray_start_local);
mul_mat3_m4_v3(imat, ray_normal_local);
/* local scale in normal direction */
local_scale = normalize_v3(ray_normal_local);
local_depth = *ray_depth;
if (local_depth != BVH_RAYCAST_DIST_MAX) {
local_depth *= local_scale;
}
if (do_bb) {
BoundBox *bb = BKE_object_boundbox_get(ob);
if (bb) {
BoundBox bb_temp;
/* We cannot aford a bbox with some null dimension, which may happen in some cases...
* Threshold is rather high, but seems to be needed to get good behavior, see T46099. */
bb = BKE_boundbox_ensure_minimum_dimensions(bb, &bb_temp, 1e-1f);
/* Exact value here is arbitrary (ideally we would scale in pixel-space based on 'dist_px'),
* scale up so we can snap against verts & edges on the boundbox, see T46816. */
if (ELEM(snap_to, SCE_SNAP_MODE_VERTEX, SCE_SNAP_MODE_EDGE)) {
BKE_boundbox_scale(&bb_temp, bb, 1.0f + 1e-1f);
bb = &bb_temp;
}
/* was local_depth, see: T47838 */
len_diff = BVH_RAYCAST_DIST_MAX;
if (!BKE_boundbox_ray_hit_check(bb, ray_start_local, ray_normal_local, &len_diff)) {
return retval;
}
need_ray_start_correction_init = false;
}
}
SnapObjectData_Mesh *sod = NULL;
BVHTreeFromMesh *treedata = NULL, treedata_stack;
if (sctx->flag & SNAP_OBJECT_USE_CACHE) {
void **sod_p;
if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) {
sod = *sod_p;
}
else {
sod = *sod_p = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*sod));
sod->sd.type = SNAP_MESH;
}
int tree_index = -1;
switch (snap_to) {
case SCE_SNAP_MODE_FACE:
tree_index = 1;
break;
case SCE_SNAP_MODE_VERTEX:
tree_index = 0;
break;
}
if (tree_index != -1) {
if (sod->bvh_trees[tree_index] == NULL) {
sod->bvh_trees[tree_index] = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*treedata));
}
treedata = sod->bvh_trees[tree_index];
/* the tree is owned by the DM and may have been freed since we last used! */
if (treedata && treedata->tree) {
if (treedata->cached && !bvhcache_has_tree(&dm->bvhCache, treedata->tree)) {
free_bvhtree_from_mesh(treedata);
}
}
}
}
else {
if (ELEM(snap_to, SCE_SNAP_MODE_FACE, SCE_SNAP_MODE_VERTEX)) {
treedata = &treedata_stack;
memset(treedata, 0, sizeof(*treedata));
}
}
if (treedata && treedata->tree == NULL) {
switch (snap_to) {
case SCE_SNAP_MODE_FACE:
bvhtree_from_mesh_looptri(treedata, dm, 0.0f, 4, 6);
break;
case SCE_SNAP_MODE_VERTEX:
bvhtree_from_mesh_verts(treedata, dm, 0.0f, 2, 6);
break;
}
}
if (need_ray_start_correction_init) {
/* We *need* a reasonably valid len_diff in this case.
* Use BHVTree to find the closest face from ray_start_local.
*/
if (treedata && treedata->tree != NULL) {
BVHTreeNearest nearest;
nearest.index = -1;
nearest.dist_sq = FLT_MAX;
/* Compute and store result. */
BLI_bvhtree_find_nearest(
treedata->tree, ray_start_local, &nearest, treedata->nearest_callback, treedata);
if (nearest.index != -1) {
len_diff = sqrtf(nearest.dist_sq);
}
}
}
/* Only use closer ray_start in case of ortho view! In perspective one, ray_start may already
* been *inside* boundbox, leading to snap failures (see T38409).
* Note also ar might be null (see T38435), in this case we assume ray_start is ok!
*/
if (do_ray_start_correction) {
float ray_org_local[3];
copy_v3_v3(ray_org_local, ray_origin);
mul_m4_v3(imat, ray_org_local);
/* We pass a temp ray_start, set from object's boundbox, to avoid precision issues with very far
* away ray_start values (as returned in case of ortho view3d), see T38358.
*/
len_diff -= local_scale; /* make temp start point a bit away from bbox hit point. */
madd_v3_v3v3fl(ray_start_local, ray_org_local, ray_normal_local,
len_diff - len_v3v3(ray_start_local, ray_org_local));
local_depth -= len_diff;
}
else {
len_diff = 0.0f;
}
switch (snap_to) {
case SCE_SNAP_MODE_FACE:
{
if (r_hit_list) {
struct RayCastAll_Data data;
data.bvhdata = treedata;
data.raycast_callback = treedata->raycast_callback;
data.obmat = obmat;
data.timat = timat;
data.len_diff = len_diff;
data.local_scale = local_scale;
data.ob = ob;
data.ob_uuid = ob_index,
data.dm = dm;
data.hit_list = r_hit_list;
data.retval = retval;
BLI_bvhtree_ray_cast_all(
treedata->tree, ray_start_local, ray_normal_local, 0.0f,
*ray_depth, raycast_all_cb, &data);
retval = data.retval;
}
else {
BVHTreeRayHit hit;
hit.index = -1;
hit.dist = local_depth;
if (treedata->tree &&
BLI_bvhtree_ray_cast(
treedata->tree, ray_start_local, ray_normal_local, 0.0f,
&hit, treedata->raycast_callback, treedata) != -1)
{
hit.dist += len_diff;
hit.dist /= local_scale;
if (hit.dist <= *ray_depth) {
*ray_depth = hit.dist;
copy_v3_v3(r_loc, hit.co);
copy_v3_v3(r_no, hit.no);
/* back to worldspace */
mul_m4_v3(obmat, r_loc);
mul_m3_v3(timat, r_no);
normalize_v3(r_no);
retval = true;
if (r_index) {
*r_index = dm_looptri_to_poly_index(dm, &treedata->looptri[hit.index]);
}
}
}
}
break;
}
case SCE_SNAP_MODE_VERTEX:
{
BVHTreeNearest nearest;
nearest.index = -1;
nearest.dist_sq = local_depth * local_depth;
if (treedata->tree &&
BLI_bvhtree_find_nearest_to_ray(
treedata->tree, ray_start_local, ray_normal_local,
&nearest, NULL, NULL) != -1)
{
const MVert *v = &treedata->vert[nearest.index];
float vno[3];
normal_short_to_float_v3(vno, v->no);
retval = snapVertex(
ar, v->co, vno, obmat, timat, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, r_no);
}
break;
}
case SCE_SNAP_MODE_EDGE:
{
MVert *verts = dm->getVertArray(dm);
MEdge *edges = dm->getEdgeArray(dm);
int totedge = dm->getNumEdges(dm);
for (int i = 0; i < totedge; i++) {
MEdge *e = edges + i;
retval |= snapEdge(
ar, verts[e->v1].co, verts[e->v1].no, verts[e->v2].co, verts[e->v2].no,
obmat, timat, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, r_no);
}
break;
}
}
if ((sctx->flag & SNAP_OBJECT_USE_CACHE) == 0) {
if (treedata) {
free_bvhtree_from_mesh(treedata);
}
}
}
return retval;
}
static bool snapEditMesh(
SnapObjectContext *sctx,
Object *ob, BMEditMesh *em, float obmat[4][4],
const float mval[2], float *dist_px, const short snap_to,
const float ray_start[3], const float ray_normal[3], const float ray_origin[3],
float *ray_depth, const unsigned int ob_index,
float r_loc[3], float r_no[3], int *r_index,
ListBase *r_hit_list)
{
ARegion *ar = sctx->v3d_data.ar;
bool retval = false;
if (snap_to == SCE_SNAP_MODE_FACE) {
if (em->bm->totface == 0) {
return retval;
}
}
if (snap_to == SCE_SNAP_MODE_EDGE) {
if (em->bm->totedge == 0) {
return retval;
}
}
else {
if (em->bm->totvert == 0) {
return retval;
}
}
{
const bool do_ray_start_correction = (
ELEM(snap_to, SCE_SNAP_MODE_FACE, SCE_SNAP_MODE_VERTEX) &&
(sctx->use_v3d && !((RegionView3D *)sctx->v3d_data.ar->regiondata)->is_persp));
float imat[4][4];
float timat[3][3]; /* transpose inverse matrix for normals */
float ray_start_local[3], ray_normal_local[3];
float local_scale, local_depth, len_diff;
invert_m4_m4(imat, obmat);
transpose_m3_m4(timat, imat);
copy_v3_v3(ray_start_local, ray_start);
copy_v3_v3(ray_normal_local, ray_normal);
mul_m4_v3(imat, ray_start_local);
mul_mat3_m4_v3(imat, ray_normal_local);
/* local scale in normal direction */
local_scale = normalize_v3(ray_normal_local);
local_depth = *ray_depth;
if (local_depth != BVH_RAYCAST_DIST_MAX) {
local_depth *= local_scale;
}
SnapObjectData_EditMesh *sod = NULL;
BVHTreeFromEditMesh *treedata = NULL, treedata_stack;
if (sctx->flag & SNAP_OBJECT_USE_CACHE) {
void **sod_p;
if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) {
sod = *sod_p;
}
else {
sod = *sod_p = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*sod));
sod->sd.type = SNAP_EDIT_MESH;
}
int tree_index = -1;
switch (snap_to) {
case SCE_SNAP_MODE_FACE:
tree_index = 1;
break;
case SCE_SNAP_MODE_VERTEX:
tree_index = 0;
break;
}
if (tree_index != -1) {
if (sod->bvh_trees[tree_index] == NULL) {
sod->bvh_trees[tree_index] = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*treedata));
}
treedata = sod->bvh_trees[tree_index];
}
}
else {
if (ELEM(snap_to, SCE_SNAP_MODE_FACE, SCE_SNAP_MODE_VERTEX)) {
treedata = &treedata_stack;
memset(treedata, 0, sizeof(*treedata));
}
}
if (treedata && treedata->tree == NULL) {
switch (snap_to) {
case SCE_SNAP_MODE_FACE:
{
BLI_bitmap *looptri_mask = NULL;
int looptri_num_active = -1;
if (sctx->callbacks.edit_mesh.test_face_fn) {
looptri_mask = BLI_BITMAP_NEW(em->tottri, __func__);
looptri_num_active = BM_iter_mesh_bitmap_from_filter_tessface(
em->bm, looptri_mask,
sctx->callbacks.edit_mesh.test_face_fn, sctx->callbacks.edit_mesh.user_data);
}
bvhtree_from_editmesh_looptri_ex(treedata, em, looptri_mask, looptri_num_active, 0.0f, 4, 6);
if (looptri_mask) {
MEM_freeN(looptri_mask);
}
break;
}
case SCE_SNAP_MODE_VERTEX:
{
BLI_bitmap *verts_mask = NULL;
int verts_num_active = -1;
if (sctx->callbacks.edit_mesh.test_vert_fn) {
verts_mask = BLI_BITMAP_NEW(em->bm->totvert, __func__);
verts_num_active = BM_iter_mesh_bitmap_from_filter(
BM_VERTS_OF_MESH, em->bm, verts_mask,
(bool (*)(BMElem *, void *))sctx->callbacks.edit_mesh.test_vert_fn,
sctx->callbacks.edit_mesh.user_data);
}
bvhtree_from_editmesh_verts_ex(treedata, em, verts_mask, verts_num_active, 0.0f, 2, 6);
if (verts_mask) {
MEM_freeN(verts_mask);
}
break;
}
}
}
/* Only use closer ray_start in case of ortho view! In perspective one, ray_start may already
* been *inside* boundbox, leading to snap failures (see T38409).
* Note also ar might be null (see T38435), in this case we assume ray_start is ok!
*/
if (do_ray_start_correction) {
/* We *need* a reasonably valid len_diff in this case.
* Use BHVTree to find the closest face from ray_start_local.
*/
if (treedata && treedata->tree != NULL) {
BVHTreeNearest nearest;
nearest.index = -1;
nearest.dist_sq = FLT_MAX;
/* Compute and store result. */
BLI_bvhtree_find_nearest(
treedata->tree, ray_start_local, &nearest, treedata->nearest_callback, treedata);
if (nearest.index != -1) {
len_diff = sqrtf(nearest.dist_sq);
}
}
float ray_org_local[3];
copy_v3_v3(ray_org_local, ray_origin);
mul_m4_v3(imat, ray_org_local);
/* We pass a temp ray_start, set from object's boundbox, to avoid precision issues with very far
* away ray_start values (as returned in case of ortho view3d), see T38358.
*/
len_diff -= local_scale; /* make temp start point a bit away from bbox hit point. */
madd_v3_v3v3fl(ray_start_local, ray_org_local, ray_normal_local,
len_diff - len_v3v3(ray_start_local, ray_org_local));
local_depth -= len_diff;
}
else {
len_diff = 0.0f;
}
switch (snap_to) {
case SCE_SNAP_MODE_FACE:
{
if (r_hit_list) {
struct RayCastAll_Data data;
data.bvhdata = treedata;
data.raycast_callback = treedata->raycast_callback;
data.obmat = obmat;
data.timat = timat;
data.len_diff = len_diff;
data.local_scale = local_scale;
data.ob = ob;
data.ob_uuid = ob_index;
data.dm = NULL;
data.hit_list = r_hit_list;
data.retval = retval;
BLI_bvhtree_ray_cast_all(
treedata->tree, ray_start_local, ray_normal_local, 0.0f,
*ray_depth, raycast_all_cb, &data);
retval = data.retval;
}
else {
BVHTreeRayHit hit;
hit.index = -1;
hit.dist = local_depth;
if (treedata->tree &&
BLI_bvhtree_ray_cast(
treedata->tree, ray_start_local, ray_normal_local, 0.0f,
&hit, treedata->raycast_callback, treedata) != -1)
{
hit.dist += len_diff;
hit.dist /= local_scale;
if (hit.dist <= *ray_depth) {
*ray_depth = hit.dist;
copy_v3_v3(r_loc, hit.co);
copy_v3_v3(r_no, hit.no);
/* back to worldspace */
mul_m4_v3(obmat, r_loc);
mul_m3_v3(timat, r_no);
normalize_v3(r_no);
retval = true;
if (r_index) {
*r_index = hit.index;
}
}
}
}
break;
}
case SCE_SNAP_MODE_VERTEX:
{
BVHTreeNearest nearest;
nearest.index = -1;
nearest.dist_sq = local_depth * local_depth;
if (treedata->tree &&
BLI_bvhtree_find_nearest_to_ray(
treedata->tree, ray_start_local, ray_normal_local,
&nearest, NULL, NULL) != -1)
{
const BMVert *v = BM_vert_at_index(em->bm, nearest.index);
retval = snapVertex(
ar, v->co, v->no, obmat, timat, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, r_no);
}
break;
}
case SCE_SNAP_MODE_EDGE:
{
BM_mesh_elem_table_ensure(em->bm, BM_EDGE);
int totedge = em->bm->totedge;
for (int i = 0; i < totedge; i++) {
BMEdge *eed = BM_edge_at_index(em->bm, i);
if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) &&
!BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) &&
!BM_elem_flag_test(eed->v2, BM_ELEM_SELECT))
{
short v1no[3], v2no[3];
normal_float_to_short_v3(v1no, eed->v1->no);
normal_float_to_short_v3(v2no, eed->v2->no);
retval |= snapEdge(
ar, eed->v1->co, v1no, eed->v2->co, v2no,
obmat, timat, mval, dist_px,
ray_start, ray_start_local, ray_normal_local, ray_depth,
r_loc, r_no);
}
}
break;
}
}
if ((sctx->flag & SNAP_OBJECT_USE_CACHE) == 0) {
if (treedata) {
free_bvhtree_from_editmesh(treedata);
}
}
}
return retval;
}
static bool snapObject(
SnapObjectContext *sctx,
Object *ob, float obmat[4][4], bool use_obedit, const short snap_to,
const float mval[2], float *dist_px, const unsigned int ob_index,
const float ray_start[3], const float ray_normal[3], const float ray_origin[3],
float *ray_depth,
/* return args */
float r_loc[3], float r_no[3], int *r_index,
Object **r_ob, float r_obmat[4][4],
ListBase *r_hit_list)
{
ARegion *ar = sctx->v3d_data.ar;
bool retval = false;
if (ob->type == OB_MESH) {
BMEditMesh *em;
if (use_obedit) {
em = BKE_editmesh_from_object(ob);
retval = snapEditMesh(
sctx, ob, em, obmat, mval, dist_px, snap_to,
ray_start, ray_normal, ray_origin,
ray_depth, ob_index,
r_loc, r_no, r_index,
r_hit_list);
}
else {
/* in this case we want the mesh from the editmesh, avoids stale data. see: T45978.
* still set the 'em' to NULL, since we only want the 'dm'. */
DerivedMesh *dm;
em = BKE_editmesh_from_object(ob);
if (em) {
editbmesh_get_derived_cage_and_final(sctx->scene, ob, em, CD_MASK_BAREMESH, &dm);
}
else {
dm = mesh_get_derived_final(sctx->scene, ob, CD_MASK_BAREMESH);
}
retval = snapDerivedMesh(
sctx, ob, dm, obmat, mval, dist_px, snap_to, true,
ray_start, ray_normal, ray_origin,
ray_depth, ob_index,
r_loc, r_no, r_index, r_hit_list);
dm->release(dm);
}
}
else if (ob->type == OB_ARMATURE) {
retval = snapArmature(
ar, ob, ob->data, obmat, mval, dist_px, snap_to,
ray_start, ray_normal, ray_depth,
r_loc, r_no);
}
else if (ob->type == OB_CURVE) {
retval = snapCurve(
ar, ob, ob->data, obmat, mval, dist_px, snap_to,
ray_start, ray_normal, ray_depth,
r_loc, r_no);
}
else if (ob->type == OB_EMPTY) {
retval = snapEmpty(
ar, ob, obmat, mval, dist_px, snap_to,
ray_start, ray_normal, ray_depth,
r_loc, r_no);
}
else if (ob->type == OB_CAMERA) {
retval = snapCamera(
ar, sctx->scene, ob, obmat, mval, dist_px, snap_to,
ray_start, ray_normal, ray_depth,
r_loc, r_no);
}
if (retval) {
if (r_ob) {
*r_ob = ob;
copy_m4_m4(r_obmat, obmat);
}
}
return retval;
}
static bool snapObjectsRay(
SnapObjectContext *sctx,
SnapSelect snap_select, const short snap_to,
const float mval[2], float *dist_px,
/* special handling of active and edit objects */
Base *base_act, Object *obedit,
const float ray_start[3], const float ray_normal[3], const float ray_origin[3], float *ray_depth,
/* return args */
float r_loc[3], float r_no[3], int *r_index,
Object **r_ob, float r_obmat[4][4],
ListBase *r_hit_list)
{
Base *base;
bool retval = false;
bool snap_obedit_first = snap_select == SNAP_ALL && obedit;
unsigned int ob_index = 0;
if (snap_obedit_first) {
Object *ob = obedit;
retval |= snapObject(
sctx, ob, ob->obmat, true, snap_to,
mval, dist_px, ob_index++,
ray_start, ray_normal, ray_origin, ray_depth,
r_loc, r_no, r_index, r_ob, r_obmat, r_hit_list);
}
/* Need an exception for particle edit because the base is flagged with BA_HAS_RECALC_DATA
* which makes the loop skip it, even the derived mesh will never change
*
* To solve that problem, we do it first as an exception.
* */
base = base_act;
if (base && base->object && base->object->mode & OB_MODE_PARTICLE_EDIT) {
Object *ob = base->object;
retval |= snapObject(
sctx, ob, ob->obmat, false, snap_to,
mval, dist_px, ob_index++,
ray_start, ray_normal, ray_origin, ray_depth,
r_loc, r_no, r_index, r_ob, r_obmat, r_hit_list);
}
for (base = sctx->scene->base.first; base != NULL; base = base->next) {
if ((BASE_VISIBLE_BGMODE(sctx->v3d_data.v3d, sctx->scene, base)) &&
(base->flag & (BA_HAS_RECALC_OB | BA_HAS_RECALC_DATA)) == 0 &&
((snap_select == SNAP_NOT_SELECTED && (base->flag & (SELECT | BA_WAS_SEL)) == 0) ||
(ELEM(snap_select, SNAP_ALL, SNAP_NOT_OBEDIT) && base != base_act)))
{
Object *ob = base->object;
Object *ob_snap = ob;
bool use_obedit = false;
if (ob->transflag & OB_DUPLI) {
DupliObject *dupli_ob;
ListBase *lb = object_duplilist(sctx->bmain->eval_ctx, sctx->scene, ob);
for (dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next) {
bool use_obedit_dupli = (obedit && dupli_ob->ob->data == obedit->data);
Object *dupli_snap = (use_obedit_dupli) ? obedit : dupli_ob->ob;
retval |= snapObject(
sctx, dupli_snap, dupli_ob->mat, use_obedit_dupli, snap_to,
mval, dist_px, ob_index++,
ray_start, ray_normal, ray_origin, ray_depth,
r_loc, r_no, r_index, r_ob, r_obmat, r_hit_list);
}
free_object_duplilist(lb);
}
if (obedit) {
if ((ob == obedit) &&
(snap_obedit_first || (snap_select == SNAP_NOT_OBEDIT)))
{
continue;
}
if (ob->data == obedit->data) {
/* for linked objects, use the same object but a different matrix */
use_obedit = true;
ob_snap = obedit;
}
}
retval |= snapObject(
sctx, ob_snap, ob->obmat, use_obedit, snap_to,
mval, dist_px, ob_index++,
ray_start, ray_normal, ray_origin, ray_depth,
r_loc, r_no, r_index, r_ob, r_obmat, r_hit_list);
}
}
return retval;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Public Object Snapping API
* \{ */
SnapObjectContext *ED_transform_snap_object_context_create(
Main *bmain, Scene *scene, int flag)
{
SnapObjectContext *sctx = MEM_callocN(sizeof(*sctx), __func__);
sctx->flag = flag;
sctx->bmain = bmain;
sctx->scene = scene;
return sctx;
}
SnapObjectContext *ED_transform_snap_object_context_create_view3d(
Main *bmain, Scene *scene, int flag,
/* extra args for view3d */
ARegion *ar, View3D *v3d)
{
SnapObjectContext *sctx = ED_transform_snap_object_context_create(bmain, scene, flag);
sctx->use_v3d = true;
sctx->v3d_data.ar = ar;
sctx->v3d_data.v3d = v3d;
if (sctx->flag & SNAP_OBJECT_USE_CACHE) {
sctx->cache.object_map = BLI_ghash_ptr_new(__func__);
sctx->cache.mem_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
}
return sctx;
}
static void snap_object_data_free(void *sod_v)
{
switch (((SnapObjectData *)sod_v)->type) {
case SNAP_MESH:
{
SnapObjectData_Mesh *sod = sod_v;
for (int i = 0; i < ARRAY_SIZE(sod->bvh_trees); i++) {
if (sod->bvh_trees[i]) {
free_bvhtree_from_mesh(sod->bvh_trees[i]);
}
}
break;
}
case SNAP_EDIT_MESH:
{
SnapObjectData_EditMesh *sod = sod_v;
for (int i = 0; i < ARRAY_SIZE(sod->bvh_trees); i++) {
if (sod->bvh_trees[i]) {
free_bvhtree_from_editmesh(sod->bvh_trees[i]);
}
}
break;
}
}
}
void ED_transform_snap_object_context_destroy(SnapObjectContext *sctx)
{
if (sctx->flag & SNAP_OBJECT_USE_CACHE) {
BLI_ghash_free(sctx->cache.object_map, NULL, snap_object_data_free);
BLI_memarena_free(sctx->cache.mem_arena);
}
MEM_freeN(sctx);
}
void ED_transform_snap_object_context_set_editmesh_callbacks(
SnapObjectContext *sctx,
bool (*test_vert_fn)(BMVert *, void *user_data),
bool (*test_edge_fn)(BMEdge *, void *user_data),
bool (*test_face_fn)(BMFace *, void *user_data),
void *user_data)
{
sctx->callbacks.edit_mesh.test_vert_fn = test_vert_fn;
sctx->callbacks.edit_mesh.test_edge_fn = test_edge_fn;
sctx->callbacks.edit_mesh.test_face_fn = test_face_fn;
sctx->callbacks.edit_mesh.user_data = user_data;
}
bool ED_transform_snap_object_project_ray_ex(
SnapObjectContext *sctx,
const struct SnapObjectParams *params,
const float ray_start[3], const float ray_normal[3], float *ray_depth,
float r_loc[3], float r_no[3], int *r_index,
Object **r_ob, float r_obmat[4][4])
{
Base *base_act = params->use_object_active ? sctx->scene->basact : NULL;
Object *obedit = params->use_object_edit ? sctx->scene->obedit : NULL;
return snapObjectsRay(
sctx,
params->snap_select, params->snap_to,
NULL, NULL,
base_act, obedit,
ray_start, ray_normal, ray_start, ray_depth,
r_loc, r_no, r_index,
r_ob, r_obmat, NULL);
}
/**
* Fill in a list of all hits.
*
* \param ray_depth: Only depths in this range are considered, -1.0 for maximum.
* \param sort: Optionally sort the hits by depth.
* \param r_hit_list: List of #SnapObjectHitDepth (caller must free).
*/
bool ED_transform_snap_object_project_ray_all(
SnapObjectContext *sctx,
const struct SnapObjectParams *params,
const float ray_start[3], const float ray_normal[3],
float ray_depth, bool sort,
ListBase *r_hit_list)
{
Base *base_act = params->use_object_active ? sctx->scene->basact : NULL;
Object *obedit = params->use_object_edit ? sctx->scene->obedit : NULL;
if (ray_depth == -1.0f) {
ray_depth = BVH_RAYCAST_DIST_MAX;
}
#ifdef DEBUG
float ray_depth_prev = ray_depth;
#endif
bool retval = snapObjectsRay(
sctx,
params->snap_select, params->snap_to,
NULL, NULL,
base_act, obedit,
ray_start, ray_normal, ray_start, &ray_depth,
NULL, NULL, NULL, NULL, NULL,
r_hit_list);
/* meant to be readonly for 'all' hits, ensure it is */
#ifdef DEBUG
BLI_assert(ray_depth_prev == ray_depth);
#endif
if (sort) {
BLI_listbase_sort(r_hit_list, hit_depth_cmp);
}
return retval;
}
/**
* Convenience function for snap ray-casting.
*
* Given a ray, cast it into the scene (snapping to faces).
*
* \return Snap success
*/
static bool transform_snap_context_project_ray_impl(
SnapObjectContext *sctx,
const float ray_start[3], const float ray_normal[3], float *ray_depth,
float r_co[3], float r_no[3])
{
bool ret;
/* try snap edge, then face if it fails */
ret = ED_transform_snap_object_project_ray_ex(
sctx,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,
.snap_to = SCE_SNAP_MODE_FACE,
.use_object_edit = (sctx->scene->obedit != NULL),
},
ray_start, ray_normal, ray_depth,
r_co, r_no, NULL,
NULL, NULL);
return ret;
}
bool ED_transform_snap_object_project_ray(
SnapObjectContext *sctx,
const float ray_origin[3], const float ray_direction[3], float *ray_depth,
float r_co[3], float r_no[3])
{
float ray_depth_fallback;
if (ray_depth == NULL) {
ray_depth_fallback = BVH_RAYCAST_DIST_MAX;
ray_depth = &ray_depth_fallback;
}
float no_fallback[3];
if (r_no == NULL) {
r_no = no_fallback;
}
return transform_snap_context_project_ray_impl(
sctx,
ray_origin, ray_direction, ray_depth,
r_co, r_no);
}
static bool transform_snap_context_project_view3d_mixed_impl(
SnapObjectContext *sctx,
const struct SnapObjectParams *params,
const float mval[2], float *dist_px,
bool use_depth,
float r_co[3], float r_no[3])
{
float ray_depth = BVH_RAYCAST_DIST_MAX;
bool is_hit = false;
float r_no_dummy[3];
if (r_no == NULL) {
r_no = r_no_dummy;
}
const int elem_type[3] = {SCE_SNAP_MODE_VERTEX, SCE_SNAP_MODE_EDGE, SCE_SNAP_MODE_FACE};
BLI_assert(params->snap_to_flag != 0);
BLI_assert((params->snap_to_flag & ~(1 | 2 | 4)) == 0);
struct SnapObjectParams params_temp = *params;
for (int i = 0; i < 3; i++) {
if ((params->snap_to_flag & (1 << i)) && (is_hit == false || use_depth)) {
if (use_depth == false) {
ray_depth = BVH_RAYCAST_DIST_MAX;
}
params_temp.snap_to = elem_type[i];
if (ED_transform_snap_object_project_view3d(
sctx,
¶ms_temp,
mval, dist_px, &ray_depth,
r_co, r_no))
{
is_hit = true;
}
}
}
return is_hit;
}
/**
* Convenience function for performing snapping.
*
* Given a 2D region value, snap to vert/edge/face.
*
* \param sctx: Snap context.
* \param mval: Screenspace coordinate.
* \param dist_px: Maximum distance to snap (in pixels).
* \param use_depth: Snap to the closest element, use when using more than one snap type.
* \param r_co: hit location.
* \param r_no: hit normal (optional).
* \return Snap success
*/
bool ED_transform_snap_object_project_view3d_mixed(
SnapObjectContext *sctx,
const struct SnapObjectParams *params,
const float mval_fl[2], float *dist_px,
bool use_depth,
float r_co[3], float r_no[3])
{
return transform_snap_context_project_view3d_mixed_impl(
sctx,
params,
mval_fl, dist_px, use_depth,
r_co, r_no);
}
bool ED_transform_snap_object_project_view3d_ex(
SnapObjectContext *sctx,
const struct SnapObjectParams *params,
const float mval[2], float *dist_px,
float *ray_depth,
float r_loc[3], float r_no[3], int *r_index)
{
float ray_start[3], ray_normal[3], ray_orgigin[3];
float ray_depth_fallback;
if (ray_depth == NULL) {
ray_depth_fallback = BVH_RAYCAST_DIST_MAX;
ray_depth = &ray_depth_fallback;
}
if (!ED_view3d_win_to_ray_ex(
sctx->v3d_data.ar, sctx->v3d_data.v3d,
mval, ray_orgigin, ray_normal, ray_start, true))
{
return false;
}
Base *base_act = params->use_object_active ? sctx->scene->basact : NULL;
Object *obedit = params->use_object_edit ? sctx->scene->obedit : NULL;
return snapObjectsRay(
sctx,
params->snap_select, params->snap_to,
mval, dist_px,
base_act, obedit,
ray_start, ray_normal, ray_orgigin, ray_depth,
r_loc, r_no, r_index, NULL, NULL, NULL);
}
bool ED_transform_snap_object_project_view3d(
SnapObjectContext *sctx,
const struct SnapObjectParams *params,
const float mval[2], float *dist_px,
float *ray_depth,
float r_loc[3], float r_no[3])
{
return ED_transform_snap_object_project_view3d_ex(
sctx,
params,
mval, dist_px,
ray_depth,
r_loc, r_no, NULL);
}
/**
* see: #ED_transform_snap_object_project_ray_all
*/
bool ED_transform_snap_object_project_all_view3d_ex(
SnapObjectContext *sctx,
const struct SnapObjectParams *params,
const float mval[2],
float ray_depth, bool sort,
ListBase *r_hit_list)
{
float ray_start[3], ray_normal[3];
BLI_assert(params->snap_to == SCE_SNAP_MODE_FACE);
if (!ED_view3d_win_to_ray_ex(
sctx->v3d_data.ar, sctx->v3d_data.v3d,
mval, NULL, ray_normal, ray_start, true))
{
return false;
}
return ED_transform_snap_object_project_ray_all(
sctx,
params,
ray_start, ray_normal, ray_depth, sort,
r_hit_list);
}
/** \} */
|