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
|
/*
* 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.
*
* The Original Code is Copyright (C) 2006 by Nicholas Bishop
* All rights reserved.
* Implements the Sculpt Mode tools
*/
/** \file
* \ingroup edsculpt
*/
#include <stddef.h>
#include "MEM_guardedalloc.h"
#include "BLI_ghash.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_string.h"
#include "BLI_task.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "BKE_ccg.h"
#include "BKE_context.h"
#include "BKE_customdata.h"
#include "BKE_global.h"
#include "BKE_key.h"
#include "BKE_main.h"
#include "BKE_mesh.h"
#include "BKE_multires.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_scene.h"
#include "BKE_subdiv_ccg.h"
#include "BKE_subsurf.h"
#include "BKE_undo_system.h"
// XXX: Ideally should be no direct call to such low level things.
#include "BKE_subdiv_eval.h"
#include "DEG_depsgraph.h"
#include "WM_api.h"
#include "WM_types.h"
#include "ED_object.h"
#include "ED_sculpt.h"
#include "ED_undo.h"
#include "bmesh.h"
#include "sculpt_intern.h"
/* Implementation of undo system for objects in sculpt mode.
*
* Each undo step in sculpt mode consists of list of nodes, each node contains:
* - Node type
* - Data for this type.
*
* Node type used for undo depends on specific operation and active sculpt mode
* ("regular" or dynamic topology).
*
* Regular sculpt brushes will use COORDS, HIDDEN or MASK nodes. These nodes are
* created for every BVH node which is affected by the brush. The undo push for
* the node happens BEFORE modifications. This makes the operation undo to work
* in the following way: for every node in the undo step swap happens between
* node in the undo stack and the corresponding value in the BVH. This is how
* redo is possible after undo.
*
* The COORDS, HIDDEN or MASK type of nodes contains arrays of the corresponding
* values.
*
* Operations like Symmetrize are using GEOMETRY type of nodes which pushes the
* entire state of the mesh to the undo stack. This node contains all CustomData
* layers.
*
* The tricky aspect of this undo node type is that it stores mesh before and
* after modification. This allows the undo system to both undo and redo the
* symmetrize operation within the pre-modified-push of other node type
* behavior, but it uses more memory that it seems it should be.
*
* The dynamic topology undo nodes are handled somewhat separately from all
* other ones and the idea there is to store log of operations: which vertices
* and faces have been added or removed.
*
* Begin of dynamic topology sculpting mode have own node type. It contains an
* entire copy of mesh since just enabling the dynamic topology mode already
* does modifications on it.
*
* End of dynamic topology and symmetrize in this mode are handled in a special
* manner as well. */
typedef struct UndoSculpt {
ListBase nodes;
size_t undo_size;
} UndoSculpt;
static UndoSculpt *sculpt_undo_get_nodes(void);
static void update_cb(PBVHNode *node, void *rebuild)
{
BKE_pbvh_node_mark_update(node);
BKE_pbvh_node_mark_update_mask(node);
if (*((bool *)rebuild)) {
BKE_pbvh_node_mark_update_visibility(node);
}
BKE_pbvh_node_fully_hidden_set(node, 0);
}
struct PartialUpdateData {
PBVH *pbvh;
bool rebuild;
char *modified_grids;
};
/**
* A version of #update_cb that tests for 'ME_VERT_PBVH_UPDATE'
*/
static void update_cb_partial(PBVHNode *node, void *userdata)
{
struct PartialUpdateData *data = userdata;
if (BKE_pbvh_type(data->pbvh) == PBVH_GRIDS) {
int *node_grid_indices;
int totgrid;
bool update = false;
BKE_pbvh_node_get_grids(data->pbvh, node, &node_grid_indices, &totgrid, NULL, NULL, NULL);
for (int i = 0; i < totgrid; i++) {
if (data->modified_grids[node_grid_indices[i]] == 1) {
update = true;
}
}
if (update) {
update_cb(node, &(data->rebuild));
}
}
else {
if (BKE_pbvh_node_vert_update_check_any(data->pbvh, node)) {
update_cb(node, &(data->rebuild));
}
}
}
static bool test_swap_v3_v3(float a[3], float b[3])
{
/* No need for float comparison here (memory is exactly equal or not). */
if (memcmp(a, b, sizeof(float[3])) != 0) {
swap_v3_v3(a, b);
return true;
}
else {
return false;
}
}
static bool sculpt_undo_restore_deformed(
const SculptSession *ss, SculptUndoNode *unode, int uindex, int oindex, float coord[3])
{
if (test_swap_v3_v3(coord, unode->orig_co[uindex])) {
copy_v3_v3(unode->co[uindex], ss->deform_cos[oindex]);
return true;
}
else {
return false;
}
}
static bool sculpt_undo_restore_coords(bContext *C, Depsgraph *depsgraph, SculptUndoNode *unode)
{
ViewLayer *view_layer = CTX_data_view_layer(C);
Object *ob = OBACT(view_layer);
SculptSession *ss = ob->sculpt;
SubdivCCG *subdiv_ccg = ss->subdiv_ccg;
MVert *mvert;
int *index;
if (unode->maxvert) {
/* Regular mesh restore. */
if (ss->shapekey_active && !STREQ(ss->shapekey_active->name, unode->shapeName)) {
/* Shape key has been changed before calling undo operator. */
Key *key = BKE_key_from_object(ob);
KeyBlock *kb = key ? BKE_keyblock_find_name(key, unode->shapeName) : NULL;
if (kb) {
ob->shapenr = BLI_findindex(&key->block, kb) + 1;
BKE_sculpt_update_object_for_edit(depsgraph, ob, false, false);
WM_event_add_notifier(C, NC_OBJECT | ND_DATA, ob);
}
else {
/* Key has been removed -- skip this undo node. */
return false;
}
}
/* No need for float comparison here (memory is exactly equal or not). */
index = unode->index;
mvert = ss->mvert;
if (ss->shapekey_active) {
float(*vertCos)[3];
vertCos = BKE_keyblock_convert_to_vertcos(ob, ss->shapekey_active);
if (unode->orig_co) {
if (ss->deform_modifiers_active) {
for (int i = 0; i < unode->totvert; i++) {
sculpt_undo_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
}
}
else {
for (int i = 0; i < unode->totvert; i++) {
swap_v3_v3(vertCos[index[i]], unode->orig_co[i]);
}
}
}
else {
for (int i = 0; i < unode->totvert; i++) {
swap_v3_v3(vertCos[index[i]], unode->co[i]);
}
}
/* Propagate new coords to keyblock. */
SCULPT_vertcos_to_key(ob, ss->shapekey_active, vertCos);
/* PBVH uses it's own mvert array, so coords should be */
/* propagated to PBVH here. */
BKE_pbvh_vert_coords_apply(ss->pbvh, vertCos, ss->shapekey_active->totelem);
MEM_freeN(vertCos);
}
else {
if (unode->orig_co) {
if (ss->deform_modifiers_active) {
for (int i = 0; i < unode->totvert; i++) {
sculpt_undo_restore_deformed(ss, unode, i, index[i], mvert[index[i]].co);
mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
}
}
else {
for (int i = 0; i < unode->totvert; i++) {
swap_v3_v3(mvert[index[i]].co, unode->orig_co[i]);
mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
}
}
}
else {
for (int i = 0; i < unode->totvert; i++) {
swap_v3_v3(mvert[index[i]].co, unode->co[i]);
mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
}
}
}
}
else if (unode->maxgrid && subdiv_ccg != NULL) {
/* Multires restore. */
CCGElem **grids, *grid;
CCGKey key;
float(*co)[3];
int gridsize;
grids = subdiv_ccg->grids;
gridsize = subdiv_ccg->grid_size;
BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
co = unode->co;
for (int j = 0; j < unode->totgrid; j++) {
grid = grids[unode->grids[j]];
for (int i = 0; i < gridsize * gridsize; i++, co++) {
swap_v3_v3(CCG_elem_offset_co(&key, grid, i), co[0]);
}
}
}
return true;
}
static bool sculpt_undo_restore_hidden(bContext *C, SculptUndoNode *unode)
{
ViewLayer *view_layer = CTX_data_view_layer(C);
Object *ob = OBACT(view_layer);
SculptSession *ss = ob->sculpt;
SubdivCCG *subdiv_ccg = ss->subdiv_ccg;
if (unode->maxvert) {
MVert *mvert = ss->mvert;
for (int i = 0; i < unode->totvert; i++) {
MVert *v = &mvert[unode->index[i]];
if ((BLI_BITMAP_TEST(unode->vert_hidden, i) != 0) != ((v->flag & ME_HIDE) != 0)) {
BLI_BITMAP_FLIP(unode->vert_hidden, i);
v->flag ^= ME_HIDE;
v->flag |= ME_VERT_PBVH_UPDATE;
}
}
}
else if (unode->maxgrid && subdiv_ccg != NULL) {
BLI_bitmap **grid_hidden = subdiv_ccg->grid_hidden;
for (int i = 0; i < unode->totgrid; i++) {
SWAP(BLI_bitmap *, unode->grid_hidden[i], grid_hidden[unode->grids[i]]);
}
}
return true;
}
static bool sculpt_undo_restore_mask(bContext *C, SculptUndoNode *unode)
{
ViewLayer *view_layer = CTX_data_view_layer(C);
Object *ob = OBACT(view_layer);
SculptSession *ss = ob->sculpt;
SubdivCCG *subdiv_ccg = ss->subdiv_ccg;
MVert *mvert;
float *vmask;
int *index;
if (unode->maxvert) {
/* Regular mesh restore. */
index = unode->index;
mvert = ss->mvert;
vmask = ss->vmask;
for (int i = 0; i < unode->totvert; i++) {
if (vmask[index[i]] != unode->mask[i]) {
SWAP(float, vmask[index[i]], unode->mask[i]);
mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
}
}
}
else if (unode->maxgrid && subdiv_ccg != NULL) {
/* Multires restore. */
CCGElem **grids, *grid;
CCGKey key;
float *mask;
int gridsize;
grids = subdiv_ccg->grids;
gridsize = subdiv_ccg->grid_size;
BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
mask = unode->mask;
for (int j = 0; j < unode->totgrid; j++) {
grid = grids[unode->grids[j]];
for (int i = 0; i < gridsize * gridsize; i++, mask++) {
SWAP(float, *CCG_elem_offset_mask(&key, grid, i), *mask);
}
}
}
return true;
}
static bool sculpt_undo_restore_face_sets(bContext *C, SculptUndoNode *unode)
{
ViewLayer *view_layer = CTX_data_view_layer(C);
Object *ob = OBACT(view_layer);
Mesh *me = BKE_object_get_original_mesh(ob);
int *face_sets = CustomData_get_layer(&me->pdata, CD_SCULPT_FACE_SETS);
for (int i = 0; i < me->totpoly; i++) {
face_sets[i] = unode->face_sets[i];
}
return false;
}
static void sculpt_undo_bmesh_restore_generic_task_cb(
void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
{
PBVHNode **nodes = userdata;
BKE_pbvh_node_mark_redraw(nodes[n]);
}
static void sculpt_undo_bmesh_restore_generic(bContext *C,
SculptUndoNode *unode,
Object *ob,
SculptSession *ss)
{
if (unode->applied) {
BM_log_undo(ss->bm, ss->bm_log);
unode->applied = false;
}
else {
BM_log_redo(ss->bm, ss->bm_log);
unode->applied = true;
}
if (unode->type == SCULPT_UNDO_MASK) {
int totnode;
PBVHNode **nodes;
Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
PBVHParallelSettings settings;
BKE_pbvh_parallel_range_settings(&settings, (sd->flags & SCULPT_USE_OPENMP), totnode);
BKE_pbvh_parallel_range(
0, totnode, nodes, sculpt_undo_bmesh_restore_generic_task_cb, &settings);
if (nodes) {
MEM_freeN(nodes);
}
}
else {
SCULPT_pbvh_clear(ob);
}
}
/* Create empty sculpt BMesh and enable logging. */
static void sculpt_undo_bmesh_enable(Object *ob, SculptUndoNode *unode)
{
SculptSession *ss = ob->sculpt;
Mesh *me = ob->data;
SCULPT_pbvh_clear(ob);
/* Create empty BMesh and enable logging. */
ss->bm = BM_mesh_create(&bm_mesh_allocsize_default,
&((struct BMeshCreateParams){
.use_toolflags = false,
}));
BM_data_layer_add(ss->bm, &ss->bm->vdata, CD_PAINT_MASK);
SCULPT_dyntopo_node_layers_add(ss);
me->flag |= ME_SCULPT_DYNAMIC_TOPOLOGY;
/* Restore the BMLog using saved entries. */
ss->bm_log = BM_log_from_existing_entries_create(ss->bm, unode->bm_entry);
}
static void sculpt_undo_bmesh_restore_begin(bContext *C,
SculptUndoNode *unode,
Object *ob,
SculptSession *ss)
{
if (unode->applied) {
SCULPT_dynamic_topology_disable(C, unode);
unode->applied = false;
}
else {
sculpt_undo_bmesh_enable(ob, unode);
/* Restore the mesh from the first log entry. */
BM_log_redo(ss->bm, ss->bm_log);
unode->applied = true;
}
}
static void sculpt_undo_bmesh_restore_end(bContext *C,
SculptUndoNode *unode,
Object *ob,
SculptSession *ss)
{
if (unode->applied) {
sculpt_undo_bmesh_enable(ob, unode);
/* Restore the mesh from the last log entry. */
BM_log_undo(ss->bm, ss->bm_log);
unode->applied = false;
}
else {
/* Disable dynamic topology sculpting. */
SCULPT_dynamic_topology_disable(C, NULL);
unode->applied = true;
}
}
static void sculpt_undo_geometry_store_data(SculptUndoNodeGeometry *geometry, Object *object)
{
Mesh *mesh = object->data;
BLI_assert(!geometry->is_initialized);
geometry->is_initialized = true;
CustomData_copy(&mesh->vdata, &geometry->vdata, CD_MASK_MESH.vmask, CD_DUPLICATE, mesh->totvert);
CustomData_copy(&mesh->edata, &geometry->edata, CD_MASK_MESH.emask, CD_DUPLICATE, mesh->totedge);
CustomData_copy(&mesh->ldata, &geometry->ldata, CD_MASK_MESH.lmask, CD_DUPLICATE, mesh->totloop);
CustomData_copy(&mesh->pdata, &geometry->pdata, CD_MASK_MESH.pmask, CD_DUPLICATE, mesh->totpoly);
geometry->totvert = mesh->totvert;
geometry->totedge = mesh->totedge;
geometry->totloop = mesh->totloop;
geometry->totpoly = mesh->totpoly;
}
static void sculpt_undo_geometry_restore_data(SculptUndoNodeGeometry *geometry, Object *object)
{
Mesh *mesh = object->data;
BLI_assert(geometry->is_initialized);
CustomData_free(&mesh->vdata, mesh->totvert);
CustomData_free(&mesh->edata, mesh->totedge);
CustomData_free(&mesh->fdata, mesh->totface);
CustomData_free(&mesh->ldata, mesh->totloop);
CustomData_free(&mesh->pdata, mesh->totpoly);
mesh->totvert = geometry->totvert;
mesh->totedge = geometry->totedge;
mesh->totloop = geometry->totloop;
mesh->totpoly = geometry->totpoly;
mesh->totface = 0;
CustomData_copy(
&geometry->vdata, &mesh->vdata, CD_MASK_MESH.vmask, CD_DUPLICATE, geometry->totvert);
CustomData_copy(
&geometry->edata, &mesh->edata, CD_MASK_MESH.emask, CD_DUPLICATE, geometry->totedge);
CustomData_copy(
&geometry->ldata, &mesh->ldata, CD_MASK_MESH.lmask, CD_DUPLICATE, geometry->totloop);
CustomData_copy(
&geometry->pdata, &mesh->pdata, CD_MASK_MESH.pmask, CD_DUPLICATE, geometry->totpoly);
BKE_mesh_update_customdata_pointers(mesh, false);
}
static void sculpt_undo_geometry_free_data(SculptUndoNodeGeometry *geometry)
{
if (geometry->totvert) {
CustomData_free(&geometry->vdata, geometry->totvert);
}
if (geometry->totedge) {
CustomData_free(&geometry->edata, geometry->totedge);
}
if (geometry->totloop) {
CustomData_free(&geometry->ldata, geometry->totloop);
}
if (geometry->totpoly) {
CustomData_free(&geometry->pdata, geometry->totpoly);
}
}
static void sculpt_undo_geometry_restore(SculptUndoNode *unode, Object *object)
{
if (unode->geometry_clear_pbvh) {
SCULPT_pbvh_clear(object);
}
if (unode->applied) {
sculpt_undo_geometry_restore_data(&unode->geometry_modified, object);
unode->applied = false;
}
else {
sculpt_undo_geometry_restore_data(&unode->geometry_original, object);
unode->applied = true;
}
}
/* Handle all dynamic-topology updates
*
* Returns true if this was a dynamic-topology undo step, otherwise
* returns false to indicate the non-dyntopo code should run. */
static int sculpt_undo_bmesh_restore(bContext *C,
SculptUndoNode *unode,
Object *ob,
SculptSession *ss)
{
switch (unode->type) {
case SCULPT_UNDO_DYNTOPO_BEGIN:
sculpt_undo_bmesh_restore_begin(C, unode, ob, ss);
return true;
case SCULPT_UNDO_DYNTOPO_END:
sculpt_undo_bmesh_restore_end(C, unode, ob, ss);
return true;
default:
if (ss->bm_log) {
sculpt_undo_bmesh_restore_generic(C, unode, ob, ss);
return true;
}
break;
}
return false;
}
static void sculpt_undo_restore_list(bContext *C, Depsgraph *depsgraph, ListBase *lb)
{
Scene *scene = CTX_data_scene(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
View3D *v3d = CTX_wm_view3d(C);
Object *ob = OBACT(view_layer);
SculptSession *ss = ob->sculpt;
SubdivCCG *subdiv_ccg = ss->subdiv_ccg;
SculptUndoNode *unode;
bool update = false, rebuild = false, update_mask = false, update_visibility = false;
bool need_mask = false;
for (unode = lb->first; unode; unode = unode->next) {
/* Restore pivot. */
copy_v3_v3(ss->pivot_pos, unode->pivot_pos);
copy_v3_v3(ss->pivot_rot, unode->pivot_rot);
if (STREQ(unode->idname, ob->id.name)) {
if (unode->type == SCULPT_UNDO_MASK) {
/* Is possible that we can't do the mask undo (below)
* because of the vertex count. */
need_mask = true;
break;
}
}
}
DEG_id_tag_update(&ob->id, ID_RECALC_SHADING);
if (lb->first) {
unode = lb->first;
if (unode->type == SCULPT_UNDO_FACE_SETS) {
sculpt_undo_restore_face_sets(C, unode);
rebuild = true;
BKE_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
BKE_sculpt_update_object_for_edit(depsgraph, ob, true, need_mask);
SCULPT_visibility_sync_all_face_sets_to_vertices(ss);
BKE_pbvh_update_vertex_data(ss->pbvh, PBVH_UpdateVisibility);
if (BKE_pbvh_type(ss->pbvh) == PBVH_FACES) {
BKE_mesh_flush_hidden_from_verts(ob->data);
}
DEG_id_tag_update(&ob->id, ID_RECALC_SHADING);
if (!BKE_sculptsession_use_pbvh_draw(ob, v3d)) {
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
}
unode->applied = true;
return;
}
}
if (lb->first != NULL) {
/* Only do early object update for edits if first node needs this.
* Undo steps like geometry does not need object to be updated before they run and will
* ensure object is updated after the node is handled. */
const SculptUndoNode *first_unode = (const SculptUndoNode *)lb->first;
if (first_unode->type != SCULPT_UNDO_GEOMETRY) {
BKE_sculpt_update_object_for_edit(depsgraph, ob, false, need_mask);
}
if (sculpt_undo_bmesh_restore(C, lb->first, ob, ss)) {
return;
}
}
char *undo_modified_grids = NULL;
bool use_multires_undo = false;
for (unode = lb->first; unode; unode = unode->next) {
if (!STREQ(unode->idname, ob->id.name)) {
continue;
}
/* Check if undo data matches current data well enough to
* continue. */
if (unode->maxvert) {
if (ss->totvert != unode->maxvert) {
continue;
}
}
else if (unode->maxgrid && subdiv_ccg != NULL) {
if ((subdiv_ccg->num_grids != unode->maxgrid) ||
(subdiv_ccg->grid_size != unode->gridsize)) {
continue;
}
use_multires_undo = true;
}
switch (unode->type) {
case SCULPT_UNDO_COORDS:
if (sculpt_undo_restore_coords(C, depsgraph, unode)) {
update = true;
}
break;
case SCULPT_UNDO_HIDDEN:
if (sculpt_undo_restore_hidden(C, unode)) {
rebuild = true;
update_visibility = true;
}
break;
case SCULPT_UNDO_MASK:
if (sculpt_undo_restore_mask(C, unode)) {
update = true;
update_mask = true;
}
break;
case SCULPT_UNDO_FACE_SETS:
break;
case SCULPT_UNDO_GEOMETRY:
sculpt_undo_geometry_restore(unode, ob);
BKE_sculpt_update_object_for_edit(depsgraph, ob, false, need_mask);
break;
case SCULPT_UNDO_DYNTOPO_BEGIN:
case SCULPT_UNDO_DYNTOPO_END:
case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
BLI_assert(!"Dynamic topology should've already been handled");
break;
}
}
if (use_multires_undo) {
for (unode = lb->first; unode; unode = unode->next) {
if (!STREQ(unode->idname, ob->id.name)) {
continue;
}
if (unode->maxgrid == 0) {
continue;
}
if (undo_modified_grids == NULL) {
undo_modified_grids = MEM_callocN(sizeof(char) * unode->maxgrid, "undo_grids");
}
for (int i = 0; i < unode->totgrid; i++) {
undo_modified_grids[unode->grids[i]] = 1;
}
}
}
if (subdiv_ccg != NULL) {
BKE_subdiv_eval_refine_from_mesh(subdiv_ccg->subdiv, ob->data, NULL);
}
if (update || rebuild) {
bool tag_update = false;
/* We update all nodes still, should be more clever, but also
* needs to work correct when exiting/entering sculpt mode and
* the nodes get recreated, though in that case it could do all. */
struct PartialUpdateData data = {
.rebuild = rebuild,
.pbvh = ss->pbvh,
.modified_grids = undo_modified_grids,
};
BKE_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb_partial, &data);
BKE_pbvh_update_bounds(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw);
if (update_mask) {
BKE_pbvh_update_vertex_data(ss->pbvh, PBVH_UpdateMask);
}
if (update_visibility) {
SCULPT_visibility_sync_all_vertex_to_face_sets(ss);
BKE_pbvh_update_visibility(ss->pbvh);
}
if (BKE_sculpt_multires_active(scene, ob)) {
if (rebuild) {
multires_mark_as_modified(depsgraph, ob, MULTIRES_HIDDEN_MODIFIED);
}
else {
multires_mark_as_modified(depsgraph, ob, MULTIRES_COORDS_MODIFIED);
}
}
tag_update |= ID_REAL_USERS(ob->data) > 1 || !BKE_sculptsession_use_pbvh_draw(ob, v3d) ||
ss->shapekey_active || ss->deform_modifiers_active;
if (tag_update) {
Mesh *mesh = ob->data;
BKE_mesh_calc_normals(mesh);
BKE_sculptsession_free_deformMats(ss);
}
if (BKE_pbvh_type(ss->pbvh) == PBVH_FACES && update_visibility) {
Mesh *mesh = ob->data;
BKE_mesh_flush_hidden_from_verts(mesh);
}
if (tag_update) {
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
}
else {
SCULPT_update_object_bounding_box(ob);
}
}
MEM_SAFE_FREE(undo_modified_grids);
}
static void sculpt_undo_free_list(ListBase *lb)
{
SculptUndoNode *unode = lb->first;
while (unode != NULL) {
SculptUndoNode *unode_next = unode->next;
if (unode->co) {
MEM_freeN(unode->co);
}
if (unode->no) {
MEM_freeN(unode->no);
}
if (unode->index) {
MEM_freeN(unode->index);
}
if (unode->grids) {
MEM_freeN(unode->grids);
}
if (unode->orig_co) {
MEM_freeN(unode->orig_co);
}
if (unode->vert_hidden) {
MEM_freeN(unode->vert_hidden);
}
if (unode->grid_hidden) {
for (int i = 0; i < unode->totgrid; i++) {
if (unode->grid_hidden[i]) {
MEM_freeN(unode->grid_hidden[i]);
}
}
MEM_freeN(unode->grid_hidden);
}
if (unode->mask) {
MEM_freeN(unode->mask);
}
if (unode->bm_entry) {
BM_log_entry_drop(unode->bm_entry);
}
sculpt_undo_geometry_free_data(&unode->geometry_original);
sculpt_undo_geometry_free_data(&unode->geometry_modified);
if (unode->face_sets) {
MEM_freeN(unode->face_sets);
}
MEM_freeN(unode);
unode = unode_next;
}
}
/* Most likely we don't need this. */
#if 0
static bool sculpt_undo_cleanup(bContext *C, ListBase *lb)
{
Scene *scene = CTX_data_scene(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
Object *ob = OBACT(view_layer);
SculptUndoNode *unode;
unode = lb->first;
if (unode && !STREQ(unode->idname, ob->id.name)) {
if (unode->bm_entry) {
BM_log_cleanup_entry(unode->bm_entry);
}
return true;
}
return false;
}
#endif
SculptUndoNode *SCULPT_undo_get_node(PBVHNode *node)
{
UndoSculpt *usculpt = sculpt_undo_get_nodes();
if (usculpt == NULL) {
return NULL;
}
return BLI_findptr(&usculpt->nodes, node, offsetof(SculptUndoNode, node));
}
SculptUndoNode *SCULPT_undo_get_first_node()
{
UndoSculpt *usculpt = sculpt_undo_get_nodes();
if (usculpt == NULL) {
return NULL;
}
return usculpt->nodes.first;
}
static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh, SculptUndoNode *unode)
{
PBVHNode *node = unode->node;
BLI_bitmap **grid_hidden;
int i, *grid_indices, totgrid;
grid_hidden = BKE_pbvh_grid_hidden(pbvh);
BKE_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid, NULL, NULL, NULL);
unode->grid_hidden = MEM_mapallocN(sizeof(*unode->grid_hidden) * totgrid, "unode->grid_hidden");
for (i = 0; i < totgrid; i++) {
if (grid_hidden[grid_indices[i]]) {
unode->grid_hidden[i] = MEM_dupallocN(grid_hidden[grid_indices[i]]);
}
else {
unode->grid_hidden[i] = NULL;
}
}
}
/* Allocate node and initialize its default fields specific for the given undo type.
* Will also add the node to the list in the undo step. */
static SculptUndoNode *sculpt_undo_alloc_node_type(Object *object, SculptUndoType type)
{
SculptUndoNode *unode = MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
BLI_strncpy(unode->idname, object->id.name, sizeof(unode->idname));
unode->type = type;
UndoSculpt *usculpt = sculpt_undo_get_nodes();
BLI_addtail(&usculpt->nodes, unode);
return unode;
}
/* Will return first existing undo node of the given type.
* If such node does not exist will allocate node of this type, register it in the undo step and
* return it. */
static SculptUndoNode *sculpt_undo_find_or_alloc_node_type(Object *object, SculptUndoType type)
{
UndoSculpt *usculpt = sculpt_undo_get_nodes();
LISTBASE_FOREACH (SculptUndoNode *, unode, &usculpt->nodes) {
if (unode->type == type) {
return unode;
}
}
return sculpt_undo_alloc_node_type(object, type);
}
static SculptUndoNode *sculpt_undo_alloc_node(Object *ob, PBVHNode *node, SculptUndoType type)
{
UndoSculpt *usculpt = sculpt_undo_get_nodes();
SculptSession *ss = ob->sculpt;
int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
SculptUndoNode *unode = sculpt_undo_alloc_node_type(ob, type);
unode->node = node;
if (node) {
BKE_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
BKE_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid, &maxgrid, &gridsize, NULL);
unode->totvert = totvert;
}
else {
maxgrid = 0;
}
/* We will use this while sculpting, is mapalloc slow to access then? */
/* General TODO, fix count_alloc. */
switch (type) {
case SCULPT_UNDO_COORDS:
unode->co = MEM_mapallocN(sizeof(float[3]) * allvert, "SculptUndoNode.co");
unode->no = MEM_mapallocN(sizeof(short[3]) * allvert, "SculptUndoNode.no");
usculpt->undo_size = (sizeof(float[3]) + sizeof(short[3]) + sizeof(int)) * allvert;
break;
case SCULPT_UNDO_HIDDEN:
if (maxgrid) {
sculpt_undo_alloc_and_store_hidden(ss->pbvh, unode);
}
else {
unode->vert_hidden = BLI_BITMAP_NEW(allvert, "SculptUndoNode.vert_hidden");
}
break;
case SCULPT_UNDO_MASK:
unode->mask = MEM_mapallocN(sizeof(float) * allvert, "SculptUndoNode.mask");
usculpt->undo_size += (sizeof(float) * sizeof(int)) * allvert;
break;
case SCULPT_UNDO_DYNTOPO_BEGIN:
case SCULPT_UNDO_DYNTOPO_END:
case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
BLI_assert(!"Dynamic topology should've already been handled");
case SCULPT_UNDO_GEOMETRY:
case SCULPT_UNDO_FACE_SETS:
break;
}
if (maxgrid) {
/* Multires. */
unode->maxgrid = maxgrid;
unode->totgrid = totgrid;
unode->gridsize = gridsize;
unode->grids = MEM_mapallocN(sizeof(int) * totgrid, "SculptUndoNode.grids");
}
else {
/* Regular mesh. */
unode->maxvert = ss->totvert;
unode->index = MEM_mapallocN(sizeof(int) * allvert, "SculptUndoNode.index");
}
if (ss->deform_modifiers_active) {
unode->orig_co = MEM_callocN(allvert * sizeof(*unode->orig_co), "undoSculpt orig_cos");
}
return unode;
}
static void sculpt_undo_store_coords(Object *ob, SculptUndoNode *unode)
{
SculptSession *ss = ob->sculpt;
PBVHVertexIter vd;
BKE_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
{
copy_v3_v3(unode->co[vd.i], vd.co);
if (vd.no) {
copy_v3_v3_short(unode->no[vd.i], vd.no);
}
else {
normal_float_to_short_v3(unode->no[vd.i], vd.fno);
}
if (ss->deform_modifiers_active) {
copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
}
}
BKE_pbvh_vertex_iter_end;
}
static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
{
PBVH *pbvh = ob->sculpt->pbvh;
PBVHNode *node = unode->node;
if (unode->grids) {
/* Already stored during allocation. */
}
else {
MVert *mvert;
const int *vert_indices;
int allvert;
int i;
BKE_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
for (i = 0; i < allvert; i++) {
BLI_BITMAP_SET(unode->vert_hidden, i, mvert[vert_indices[i]].flag & ME_HIDE);
}
}
}
static void sculpt_undo_store_mask(Object *ob, SculptUndoNode *unode)
{
SculptSession *ss = ob->sculpt;
PBVHVertexIter vd;
BKE_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
{
unode->mask[vd.i] = *vd.mask;
}
BKE_pbvh_vertex_iter_end;
}
static SculptUndoNodeGeometry *sculpt_undo_geometry_get(SculptUndoNode *unode)
{
if (!unode->geometry_original.is_initialized) {
return &unode->geometry_original;
}
BLI_assert(!unode->geometry_modified.is_initialized);
return &unode->geometry_modified;
}
static SculptUndoNode *sculpt_undo_geometry_push(Object *object, SculptUndoType type)
{
SculptUndoNode *unode = sculpt_undo_find_or_alloc_node_type(object, type);
unode->applied = false;
unode->geometry_clear_pbvh = true;
SculptUndoNodeGeometry *geometry = sculpt_undo_geometry_get(unode);
sculpt_undo_geometry_store_data(geometry, object);
return unode;
}
static SculptUndoNode *sculpt_undo_face_sets_push(Object *ob, SculptUndoType type)
{
UndoSculpt *usculpt = sculpt_undo_get_nodes();
SculptUndoNode *unode = usculpt->nodes.first;
unode = MEM_callocN(sizeof(*unode), __func__);
BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
unode->type = type;
unode->applied = true;
Mesh *me = BKE_object_get_original_mesh(ob);
unode->face_sets = MEM_callocN(me->totpoly * sizeof(int), "sculpt face sets");
int *face_sets = CustomData_get_layer(&me->pdata, CD_SCULPT_FACE_SETS);
for (int i = 0; i < me->totpoly; i++) {
unode->face_sets[i] = face_sets[i];
}
BLI_addtail(&usculpt->nodes, unode);
return unode;
}
static SculptUndoNode *sculpt_undo_bmesh_push(Object *ob, PBVHNode *node, SculptUndoType type)
{
UndoSculpt *usculpt = sculpt_undo_get_nodes();
SculptSession *ss = ob->sculpt;
PBVHVertexIter vd;
SculptUndoNode *unode = usculpt->nodes.first;
if (unode == NULL) {
unode = MEM_callocN(sizeof(*unode), __func__);
BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
unode->type = type;
unode->applied = true;
if (type == SCULPT_UNDO_DYNTOPO_END) {
unode->bm_entry = BM_log_entry_add(ss->bm_log);
BM_log_before_all_removed(ss->bm, ss->bm_log);
}
else if (type == SCULPT_UNDO_DYNTOPO_BEGIN) {
/* Store a copy of the mesh's current vertices, loops, and
* polys. A full copy like this is needed because entering
* dynamic-topology immediately does topological edits
* (converting polys to triangles) that the BMLog can't
* fully restore from. */
SculptUndoNodeGeometry *geometry = &unode->geometry_bmesh_enter;
sculpt_undo_geometry_store_data(geometry, ob);
unode->bm_entry = BM_log_entry_add(ss->bm_log);
BM_log_all_added(ss->bm, ss->bm_log);
}
else {
unode->bm_entry = BM_log_entry_add(ss->bm_log);
}
BLI_addtail(&usculpt->nodes, unode);
}
if (node) {
switch (type) {
case SCULPT_UNDO_COORDS:
case SCULPT_UNDO_MASK:
/* Before any vertex values get modified, ensure their
* original positions are logged. */
BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL)
{
BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
}
BKE_pbvh_vertex_iter_end;
break;
case SCULPT_UNDO_HIDDEN: {
GSetIterator gs_iter;
GSet *faces = BKE_pbvh_bmesh_node_faces(node);
BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL)
{
BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
}
BKE_pbvh_vertex_iter_end;
GSET_ITER (gs_iter, faces) {
BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
BM_log_face_modified(ss->bm_log, f);
}
break;
}
case SCULPT_UNDO_DYNTOPO_BEGIN:
case SCULPT_UNDO_DYNTOPO_END:
case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
case SCULPT_UNDO_GEOMETRY:
case SCULPT_UNDO_FACE_SETS:
break;
}
}
return unode;
}
SculptUndoNode *SCULPT_undo_push_node(Object *ob, PBVHNode *node, SculptUndoType type)
{
SculptSession *ss = ob->sculpt;
SculptUndoNode *unode;
/* List is manipulated by multiple threads, so we lock. */
BLI_thread_lock(LOCK_CUSTOM1);
ss->needs_flush_to_id = 1;
if (ss->bm || ELEM(type, SCULPT_UNDO_DYNTOPO_BEGIN, SCULPT_UNDO_DYNTOPO_END)) {
/* Dynamic topology stores only one undo node per stroke,
* regardless of the number of PBVH nodes modified. */
unode = sculpt_undo_bmesh_push(ob, node, type);
BLI_thread_unlock(LOCK_CUSTOM1);
return unode;
}
else if (type == SCULPT_UNDO_GEOMETRY) {
unode = sculpt_undo_geometry_push(ob, type);
BLI_thread_unlock(LOCK_CUSTOM1);
return unode;
}
else if (type == SCULPT_UNDO_FACE_SETS) {
unode = sculpt_undo_face_sets_push(ob, type);
BLI_thread_unlock(LOCK_CUSTOM1);
return unode;
}
else if ((unode = SCULPT_undo_get_node(node))) {
BLI_thread_unlock(LOCK_CUSTOM1);
return unode;
}
unode = sculpt_undo_alloc_node(ob, node, type);
/* NOTE: If this ever becomes a bottleneck, make a lock inside of the node.
* so we release global lock sooner, but keep data locked for until it is
* fully initialized.
*/
if (unode->grids) {
int totgrid, *grids;
BKE_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid, NULL, NULL, NULL);
memcpy(unode->grids, grids, sizeof(int) * totgrid);
}
else {
const int *vert_indices;
int allvert;
BKE_pbvh_node_num_verts(ss->pbvh, node, NULL, &allvert);
BKE_pbvh_node_get_verts(ss->pbvh, node, &vert_indices, NULL);
memcpy(unode->index, vert_indices, sizeof(int) * unode->totvert);
}
switch (type) {
case SCULPT_UNDO_COORDS:
sculpt_undo_store_coords(ob, unode);
break;
case SCULPT_UNDO_HIDDEN:
sculpt_undo_store_hidden(ob, unode);
break;
case SCULPT_UNDO_MASK:
sculpt_undo_store_mask(ob, unode);
break;
case SCULPT_UNDO_DYNTOPO_BEGIN:
case SCULPT_UNDO_DYNTOPO_END:
case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
BLI_assert(!"Dynamic topology should've already been handled");
case SCULPT_UNDO_GEOMETRY:
case SCULPT_UNDO_FACE_SETS:
break;
}
/* Store sculpt pivot. */
copy_v3_v3(unode->pivot_pos, ss->pivot_pos);
copy_v3_v3(unode->pivot_rot, ss->pivot_rot);
/* Store active shape key. */
if (ss->shapekey_active) {
BLI_strncpy(unode->shapeName, ss->shapekey_active->name, sizeof(ss->shapekey_active->name));
}
else {
unode->shapeName[0] = '\0';
}
BLI_thread_unlock(LOCK_CUSTOM1);
return unode;
}
void SCULPT_undo_push_begin(const char *name)
{
UndoStack *ustack = ED_undo_stack_get();
/* Special case, we never read from this. */
bContext *C = NULL;
BKE_undosys_step_push_init_with_type(ustack, C, name, BKE_UNDOSYS_TYPE_SCULPT);
}
void SCULPT_undo_push_end(void)
{
SCULPT_undo_push_end_ex(false);
}
void SCULPT_undo_push_end_ex(const bool use_nested_undo)
{
UndoSculpt *usculpt = sculpt_undo_get_nodes();
SculptUndoNode *unode;
/* We don't need normals in the undo stack. */
for (unode = usculpt->nodes.first; unode; unode = unode->next) {
if (unode->no) {
MEM_freeN(unode->no);
unode->no = NULL;
}
}
/* We could remove this and enforce all callers run in an operator using 'OPTYPE_UNDO'. */
wmWindowManager *wm = G_MAIN->wm.first;
if (wm->op_undo_depth == 0 || use_nested_undo) {
UndoStack *ustack = ED_undo_stack_get();
BKE_undosys_step_push(ustack, NULL, NULL);
WM_file_tag_modified();
}
}
/* -------------------------------------------------------------------- */
/** \name Implements ED Undo System
* \{ */
typedef struct SculptUndoStep {
UndoStep step;
/* Note: will split out into list for multi-object-sculpt-mode. */
UndoSculpt data;
} SculptUndoStep;
static void sculpt_undosys_step_encode_init(struct bContext *UNUSED(C), UndoStep *us_p)
{
SculptUndoStep *us = (SculptUndoStep *)us_p;
/* Dummy, memory is cleared anyway. */
BLI_listbase_clear(&us->data.nodes);
}
static bool sculpt_undosys_step_encode(struct bContext *UNUSED(C),
struct Main *bmain,
UndoStep *us_p)
{
/* Dummy, encoding is done along the way by adding tiles
* to the current 'SculptUndoStep' added by encode_init. */
SculptUndoStep *us = (SculptUndoStep *)us_p;
us->step.data_size = us->data.undo_size;
SculptUndoNode *unode = us->data.nodes.last;
if (unode && unode->type == SCULPT_UNDO_DYNTOPO_END) {
us->step.use_memfile_step = true;
}
us->step.is_applied = true;
if (!BLI_listbase_is_empty(&us->data.nodes)) {
bmain->is_memfile_undo_flush_needed = true;
}
return true;
}
static void sculpt_undosys_step_decode_undo_impl(struct bContext *C,
Depsgraph *depsgraph,
SculptUndoStep *us)
{
BLI_assert(us->step.is_applied == true);
sculpt_undo_restore_list(C, depsgraph, &us->data.nodes);
us->step.is_applied = false;
}
static void sculpt_undosys_step_decode_redo_impl(struct bContext *C,
Depsgraph *depsgraph,
SculptUndoStep *us)
{
BLI_assert(us->step.is_applied == false);
sculpt_undo_restore_list(C, depsgraph, &us->data.nodes);
us->step.is_applied = true;
}
static void sculpt_undosys_step_decode_undo(struct bContext *C,
Depsgraph *depsgraph,
SculptUndoStep *us)
{
SculptUndoStep *us_iter = us;
while (us_iter->step.next && (us_iter->step.next->type == us_iter->step.type)) {
if (us_iter->step.next->is_applied == false) {
break;
}
us_iter = (SculptUndoStep *)us_iter->step.next;
}
while (us_iter != us) {
sculpt_undosys_step_decode_undo_impl(C, depsgraph, us_iter);
us_iter = (SculptUndoStep *)us_iter->step.prev;
}
}
static void sculpt_undosys_step_decode_redo(struct bContext *C,
Depsgraph *depsgraph,
SculptUndoStep *us)
{
SculptUndoStep *us_iter = us;
while (us_iter->step.prev && (us_iter->step.prev->type == us_iter->step.type)) {
if (us_iter->step.prev->is_applied == true) {
break;
}
us_iter = (SculptUndoStep *)us_iter->step.prev;
}
while (us_iter && (us_iter->step.is_applied == false)) {
sculpt_undosys_step_decode_redo_impl(C, depsgraph, us_iter);
if (us_iter == us) {
break;
}
us_iter = (SculptUndoStep *)us_iter->step.next;
}
}
static void sculpt_undosys_step_decode(
struct bContext *C, struct Main *bmain, UndoStep *us_p, int dir, bool UNUSED(is_final))
{
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
/* Ensure sculpt mode. */
{
Scene *scene = CTX_data_scene(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
/* Sculpt needs evaluated state. */
BKE_scene_view_layer_graph_evaluated_ensure(bmain, scene, view_layer);
Object *ob = OBACT(view_layer);
if (ob && (ob->type == OB_MESH)) {
if (ob->mode & OB_MODE_SCULPT) {
/* Pass. */
}
else {
ED_object_mode_generic_exit(bmain, depsgraph, scene, ob);
Mesh *me = ob->data;
/* Don't add sculpt topology undo steps when reading back undo state.
* The undo steps must enter/exit for us. */
me->flag &= ~ME_SCULPT_DYNAMIC_TOPOLOGY;
ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, true, NULL);
}
if (ob->sculpt) {
ob->sculpt->needs_flush_to_id = 1;
}
bmain->is_memfile_undo_flush_needed = true;
}
else {
BLI_assert(0);
return;
}
}
SculptUndoStep *us = (SculptUndoStep *)us_p;
if (dir < 0) {
sculpt_undosys_step_decode_undo(C, depsgraph, us);
}
else {
sculpt_undosys_step_decode_redo(C, depsgraph, us);
}
}
static void sculpt_undosys_step_free(UndoStep *us_p)
{
SculptUndoStep *us = (SculptUndoStep *)us_p;
sculpt_undo_free_list(&us->data.nodes);
}
void ED_sculpt_undo_geometry_begin(struct Object *ob, const char *name)
{
SCULPT_undo_push_begin(name);
SCULPT_undo_push_node(ob, NULL, SCULPT_UNDO_GEOMETRY);
}
void ED_sculpt_undo_geometry_end(struct Object *ob)
{
SCULPT_undo_push_node(ob, NULL, SCULPT_UNDO_GEOMETRY);
SCULPT_undo_push_end();
}
/* Export for ED_undo_sys. */
void ED_sculpt_undosys_type(UndoType *ut)
{
ut->name = "Sculpt";
ut->step_encode_init = sculpt_undosys_step_encode_init;
ut->step_encode = sculpt_undosys_step_encode;
ut->step_decode = sculpt_undosys_step_decode;
ut->step_free = sculpt_undosys_step_free;
ut->use_context = true;
ut->step_size = sizeof(SculptUndoStep);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Utilities
* \{ */
static UndoSculpt *sculpt_undosys_step_get_nodes(UndoStep *us_p)
{
SculptUndoStep *us = (SculptUndoStep *)us_p;
return &us->data;
}
static UndoSculpt *sculpt_undo_get_nodes(void)
{
UndoStack *ustack = ED_undo_stack_get();
UndoStep *us = BKE_undosys_stack_init_or_active_with_type(ustack, BKE_UNDOSYS_TYPE_SCULPT);
return sculpt_undosys_step_get_nodes(us);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Undo for changes happening on a base mesh for multires sculpting.
*
* Use this for multires operators which changes base mesh and which are to be
* possible. Example of such operators is Apply Base.
*
* Usage:
*
* static int operator_exec((bContext *C, wmOperator *op) {
*
* ED_sculpt_undo_push_mixed_begin(C, op->type->name);
* // Modify base mesh.
* ED_sculpt_undo_push_mixed_end(C, op->type->name);
*
* return OPERATOR_FINISHED;
* }
*
* If object is not in sculpt mode or sculpt does not happen on multires then
* regular ED_undo_push() is used.
* *
* \{ */
static bool sculpt_undo_use_multires_mesh(bContext *C)
{
if (BKE_paintmode_get_active_from_context(C) != PAINT_MODE_SCULPT) {
return false;
}
Object *object = CTX_data_active_object(C);
SculptSession *sculpt_session = object->sculpt;
return sculpt_session->multires.active;
}
static void sculpt_undo_push_all_grids(Object *object)
{
SculptSession *ss = object->sculpt;
PBVHNode **nodes;
int totnodes;
BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnodes);
for (int i = 0; i < totnodes; i++) {
SculptUndoNode *unode = SCULPT_undo_push_node(object, nodes[i], SCULPT_UNDO_COORDS);
unode->node = NULL;
}
MEM_SAFE_FREE(nodes);
}
void ED_sculpt_undo_push_multires_mesh_begin(bContext *C, const char *str)
{
if (!sculpt_undo_use_multires_mesh(C)) {
return;
}
Object *object = CTX_data_active_object(C);
SCULPT_undo_push_begin(str);
SculptUndoNode *geometry_unode = SCULPT_undo_push_node(object, NULL, SCULPT_UNDO_GEOMETRY);
geometry_unode->geometry_clear_pbvh = false;
sculpt_undo_push_all_grids(object);
}
void ED_sculpt_undo_push_multires_mesh_end(bContext *C, const char *str)
{
if (!sculpt_undo_use_multires_mesh(C)) {
ED_undo_push(C, str);
return;
}
Object *object = CTX_data_active_object(C);
SculptUndoNode *geometry_unode = SCULPT_undo_push_node(object, NULL, SCULPT_UNDO_GEOMETRY);
geometry_unode->geometry_clear_pbvh = false;
SCULPT_undo_push_end();
}
/** \} */
|