2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2006 by Nicholas Bishop
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
27 * Implements the Sculpt Mode tools
31 /** \file blender/editors/sculpt_paint/sculpt_undo.c
37 #include "MEM_guardedalloc.h"
40 #include "BLI_utildefines.h"
41 #include "BLI_string.h"
42 #include "BLI_listbase.h"
43 #include "BLI_ghash.h"
45 #include "BLI_threads.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_mesh_types.h"
53 #include "BKE_context.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_multires.h"
56 #include "BKE_paint.h"
59 #include "BKE_subsurf.h"
64 #include "GPU_buffers.h"
69 #include "paint_intern.h"
70 #include "sculpt_intern.h"
72 /************************** Undo *************************/
74 static void update_cb(PBVHNode *node, void *rebuild)
76 BKE_pbvh_node_mark_update(node);
77 if (*((bool *)rebuild))
78 BKE_pbvh_node_mark_rebuild_draw(node);
79 BKE_pbvh_node_fully_hidden_set(node, 0);
82 struct PartialUpdateData {
88 * A version of #update_cb that tests for 'ME_VERT_PBVH_UPDATE'
90 static void update_cb_partial(PBVHNode *node, void *userdata)
92 struct PartialUpdateData *data = userdata;
93 if (BKE_pbvh_node_vert_update_check_any(data->pbvh, node)) {
94 update_cb(node, &(data->rebuild));
98 static bool test_swap_v3_v3(float a[3], float b[3])
100 /* no need for float comparison here (memory is exactly equal or not) */
101 if (memcmp(a, b, sizeof(float[3])) != 0) {
110 static bool sculpt_undo_restore_deformed(
111 const SculptSession *ss,
112 SculptUndoNode *unode,
113 int uindex, int oindex,
116 if (test_swap_v3_v3(coord, unode->orig_co[uindex])) {
117 copy_v3_v3(unode->co[uindex], ss->deform_cos[oindex]);
125 static bool sculpt_undo_restore_coords(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
127 Scene *scene = CTX_data_scene(C);
128 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
129 Object *ob = CTX_data_active_object(C);
130 SculptSession *ss = ob->sculpt;
134 if (unode->maxvert) {
135 /* regular mesh restore */
137 if (ss->kb && !STREQ(ss->kb->name, unode->shapeName)) {
138 /* shape key has been changed before calling undo operator */
140 Key *key = BKE_key_from_object(ob);
141 KeyBlock *kb = key ? BKE_keyblock_find_name(key, unode->shapeName) : NULL;
144 ob->shapenr = BLI_findindex(&key->block, kb) + 1;
146 BKE_sculpt_update_mesh_elements(scene, sd, ob, 0, false);
147 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, ob);
150 /* key has been removed -- skip this undo node */
155 /* no need for float comparison here (memory is exactly equal or not) */
156 index = unode->index;
161 vertCos = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
163 if (unode->orig_co) {
164 if (ss->modifiers_active) {
165 for (int i = 0; i < unode->totvert; i++) {
166 sculpt_undo_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
170 for (int i = 0; i < unode->totvert; i++) {
171 swap_v3_v3(vertCos[index[i]], unode->orig_co[i]);
176 for (int i = 0; i < unode->totvert; i++) {
177 swap_v3_v3(vertCos[index[i]], unode->co[i]);
181 /* propagate new coords to keyblock */
182 sculpt_vertcos_to_key(ob, ss->kb, vertCos);
184 /* pbvh uses it's own mvert array, so coords should be */
185 /* propagated to pbvh here */
186 BKE_pbvh_apply_vertCos(ss->pbvh, vertCos);
191 if (unode->orig_co) {
192 if (ss->modifiers_active) {
193 for (int i = 0; i < unode->totvert; i++) {
194 if (sculpt_undo_restore_deformed(ss, unode, i, index[i], mvert[index[i]].co)) {
195 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
200 for (int i = 0; i < unode->totvert; i++) {
201 if (test_swap_v3_v3(mvert[index[i]].co, unode->orig_co[i])) {
202 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
208 for (int i = 0; i < unode->totvert; i++) {
209 if (test_swap_v3_v3(mvert[index[i]].co, unode->co[i])) {
210 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
216 else if (unode->maxgrid && dm->getGridData) {
217 /* multires restore */
218 CCGElem **grids, *grid;
223 grids = dm->getGridData(dm);
224 gridsize = dm->getGridSize(dm);
225 dm->getGridKey(dm, &key);
228 for (int j = 0; j < unode->totgrid; j++) {
229 grid = grids[unode->grids[j]];
231 for (int i = 0; i < gridsize * gridsize; i++, co++) {
232 swap_v3_v3(CCG_elem_offset_co(&key, grid, i), co[0]);
240 static bool sculpt_undo_restore_hidden(
241 bContext *C, DerivedMesh *dm,
242 SculptUndoNode *unode)
244 Object *ob = CTX_data_active_object(C);
245 SculptSession *ss = ob->sculpt;
248 if (unode->maxvert) {
249 MVert *mvert = ss->mvert;
251 for (i = 0; i < unode->totvert; i++) {
252 MVert *v = &mvert[unode->index[i]];
253 if ((BLI_BITMAP_TEST(unode->vert_hidden, i) != 0) != ((v->flag & ME_HIDE) != 0)) {
254 BLI_BITMAP_FLIP(unode->vert_hidden, i);
256 v->flag |= ME_VERT_PBVH_UPDATE;
260 else if (unode->maxgrid && dm->getGridData) {
261 BLI_bitmap **grid_hidden = dm->getGridHidden(dm);
263 for (i = 0; i < unode->totgrid; i++) {
265 unode->grid_hidden[i],
266 grid_hidden[unode->grids[i]]);
274 static bool sculpt_undo_restore_mask(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
276 Object *ob = CTX_data_active_object(C);
277 SculptSession *ss = ob->sculpt;
282 if (unode->maxvert) {
283 /* regular mesh restore */
285 index = unode->index;
289 for (i = 0; i < unode->totvert; i++) {
290 if (vmask[index[i]] != unode->mask[i]) {
291 SWAP(float, vmask[index[i]], unode->mask[i]);
292 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
296 else if (unode->maxgrid && dm->getGridData) {
297 /* multires restore */
298 CCGElem **grids, *grid;
303 grids = dm->getGridData(dm);
304 gridsize = dm->getGridSize(dm);
305 dm->getGridKey(dm, &key);
308 for (j = 0; j < unode->totgrid; j++) {
309 grid = grids[unode->grids[j]];
311 for (i = 0; i < gridsize * gridsize; i++, mask++)
312 SWAP(float, *CCG_elem_offset_mask(&key, grid, i), *mask);
319 static void sculpt_undo_bmesh_restore_generic_task_cb(
320 void *__restrict userdata,
322 const ParallelRangeTLS *__restrict UNUSED(tls))
324 PBVHNode **nodes = userdata;
326 BKE_pbvh_node_mark_redraw(nodes[n]);
329 static void sculpt_undo_bmesh_restore_generic(bContext *C,
330 SculptUndoNode *unode,
334 if (unode->applied) {
335 BM_log_undo(ss->bm, ss->bm_log);
336 unode->applied = false;
339 BM_log_redo(ss->bm, ss->bm_log);
340 unode->applied = true;
343 if (unode->type == SCULPT_UNDO_MASK) {
346 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
348 BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
350 ParallelRangeSettings settings;
351 BLI_parallel_range_settings_defaults(&settings);
352 settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
353 BLI_task_parallel_range(
356 sculpt_undo_bmesh_restore_generic_task_cb,
363 sculpt_pbvh_clear(ob);
367 /* Create empty sculpt BMesh and enable logging */
368 static void sculpt_undo_bmesh_enable(Object *ob,
369 SculptUndoNode *unode)
371 SculptSession *ss = ob->sculpt;
374 sculpt_pbvh_clear(ob);
376 /* Create empty BMesh and enable logging */
377 ss->bm = BM_mesh_create(
378 &bm_mesh_allocsize_default,
379 &((struct BMeshCreateParams){.use_toolflags = false,}));
380 BM_data_layer_add(ss->bm, &ss->bm->vdata, CD_PAINT_MASK);
381 sculpt_dyntopo_node_layers_add(ss);
382 me->flag |= ME_SCULPT_DYNAMIC_TOPOLOGY;
384 /* Restore the BMLog using saved entries */
385 ss->bm_log = BM_log_from_existing_entries_create(ss->bm,
389 static void sculpt_undo_bmesh_restore_begin(bContext *C,
390 SculptUndoNode *unode,
394 if (unode->applied) {
395 sculpt_dynamic_topology_disable(C, unode);
396 unode->applied = false;
399 sculpt_undo_bmesh_enable(ob, unode);
401 /* Restore the mesh from the first log entry */
402 BM_log_redo(ss->bm, ss->bm_log);
404 unode->applied = true;
408 static void sculpt_undo_bmesh_restore_end(bContext *C,
409 SculptUndoNode *unode,
413 if (unode->applied) {
414 sculpt_undo_bmesh_enable(ob, unode);
416 /* Restore the mesh from the last log entry */
417 BM_log_undo(ss->bm, ss->bm_log);
419 unode->applied = false;
422 /* Disable dynamic topology sculpting */
423 sculpt_dynamic_topology_disable(C, NULL);
424 unode->applied = true;
428 /* Handle all dynamic-topology updates
430 * Returns true if this was a dynamic-topology undo step, otherwise
431 * returns false to indicate the non-dyntopo code should run. */
432 static int sculpt_undo_bmesh_restore(bContext *C,
433 SculptUndoNode *unode,
437 switch (unode->type) {
438 case SCULPT_UNDO_DYNTOPO_BEGIN:
439 sculpt_undo_bmesh_restore_begin(C, unode, ob, ss);
442 case SCULPT_UNDO_DYNTOPO_END:
443 sculpt_undo_bmesh_restore_end(C, unode, ob, ss);
448 sculpt_undo_bmesh_restore_generic(C, unode, ob, ss);
457 static void sculpt_undo_restore(bContext *C, ListBase *lb)
459 Scene *scene = CTX_data_scene(C);
460 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
461 Object *ob = CTX_data_active_object(C);
463 SculptSession *ss = ob->sculpt;
464 SculptUndoNode *unode;
465 bool update = false, rebuild = false;
466 bool need_mask = false;
467 bool partial_update = true;
469 for (unode = lb->first; unode; unode = unode->next) {
470 if (STREQ(unode->idname, ob->id.name)) {
471 if (unode->type == SCULPT_UNDO_MASK) {
472 /* is possible that we can't do the mask undo (below)
473 * because of the vertex count */
480 BKE_sculpt_update_mesh_elements(scene, sd, ob, 0, need_mask);
482 /* call _after_ sculpt_update_mesh_elements() which may update 'ob->derivedFinal' */
483 dm = mesh_get_derived_final(scene, ob, 0);
485 if (lb->first && sculpt_undo_bmesh_restore(C, lb->first, ob, ss))
488 for (unode = lb->first; unode; unode = unode->next) {
489 if (!STREQ(unode->idname, ob->id.name))
492 /* check if undo data matches current data well enough to
494 if (unode->maxvert) {
495 if (ss->totvert != unode->maxvert)
498 else if (unode->maxgrid && dm->getGridData) {
499 if ((dm->getNumGrids(dm) != unode->maxgrid) ||
500 (dm->getGridSize(dm) != unode->gridsize))
505 /* multi-res can't do partial updates since it doesn't flag edited vertices */
506 partial_update = false;
509 switch (unode->type) {
510 case SCULPT_UNDO_COORDS:
511 if (sculpt_undo_restore_coords(C, dm, unode))
514 case SCULPT_UNDO_HIDDEN:
515 if (sculpt_undo_restore_hidden(C, dm, unode))
518 case SCULPT_UNDO_MASK:
519 if (sculpt_undo_restore_mask(C, dm, unode))
523 case SCULPT_UNDO_DYNTOPO_BEGIN:
524 case SCULPT_UNDO_DYNTOPO_END:
525 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
526 BLI_assert(!"Dynamic topology should've already been handled");
531 if (update || rebuild) {
532 bool tag_update = false;
533 /* we update all nodes still, should be more clever, but also
534 * needs to work correct when exiting/entering sculpt mode and
535 * the nodes get recreated, though in that case it could do all */
536 if (partial_update) {
537 struct PartialUpdateData data = {
541 BKE_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb_partial, &data);
544 BKE_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
546 BKE_pbvh_update(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw, NULL);
548 if (BKE_sculpt_multires_active(scene, ob)) {
550 multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
552 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
555 tag_update |= ((Mesh *)ob->data)->id.us > 1;
557 if (ss->kb || ss->modifiers_active) {
558 Mesh *mesh = ob->data;
559 BKE_mesh_calc_normals(mesh);
561 BKE_sculptsession_free_deformMats(ss);
566 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
569 sculpt_update_object_bounding_box(ob);
572 /* for non-PBVH drawing, need to recreate VBOs */
573 GPU_drawobject_free(ob->derivedFinal);
577 static void sculpt_undo_free(ListBase *lb)
579 SculptUndoNode *unode;
582 for (unode = lb->first; unode; unode = unode->next) {
584 MEM_freeN(unode->co);
586 MEM_freeN(unode->no);
588 MEM_freeN(unode->index);
590 MEM_freeN(unode->grids);
592 MEM_freeN(unode->orig_co);
593 if (unode->vert_hidden)
594 MEM_freeN(unode->vert_hidden);
595 if (unode->grid_hidden) {
596 for (i = 0; i < unode->totgrid; i++) {
597 if (unode->grid_hidden[i])
598 MEM_freeN(unode->grid_hidden[i]);
600 MEM_freeN(unode->grid_hidden);
603 MEM_freeN(unode->mask);
605 if (unode->bm_entry) {
606 BM_log_entry_drop(unode->bm_entry);
609 if (unode->bm_enter_totvert)
610 CustomData_free(&unode->bm_enter_vdata, unode->bm_enter_totvert);
611 if (unode->bm_enter_totedge)
612 CustomData_free(&unode->bm_enter_edata, unode->bm_enter_totedge);
613 if (unode->bm_enter_totloop)
614 CustomData_free(&unode->bm_enter_ldata, unode->bm_enter_totloop);
615 if (unode->bm_enter_totpoly)
616 CustomData_free(&unode->bm_enter_pdata, unode->bm_enter_totpoly);
620 static bool sculpt_undo_cleanup(bContext *C, ListBase *lb)
622 Object *ob = CTX_data_active_object(C);
623 SculptUndoNode *unode;
627 if (unode && !STREQ(unode->idname, ob->id.name)) {
629 BM_log_cleanup_entry(unode->bm_entry);
637 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
639 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
645 return BLI_findptr(lb, node, offsetof(SculptUndoNode, node));
648 static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh,
649 SculptUndoNode *unode)
651 PBVHNode *node = unode->node;
652 BLI_bitmap **grid_hidden;
653 int i, *grid_indices, totgrid;
655 grid_hidden = BKE_pbvh_grid_hidden(pbvh);
657 BKE_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid,
660 unode->grid_hidden = MEM_mapallocN(sizeof(*unode->grid_hidden) * totgrid,
661 "unode->grid_hidden");
663 for (i = 0; i < totgrid; i++) {
664 if (grid_hidden[grid_indices[i]])
665 unode->grid_hidden[i] = MEM_dupallocN(grid_hidden[grid_indices[i]]);
667 unode->grid_hidden[i] = NULL;
671 static SculptUndoNode *sculpt_undo_alloc_node(Object *ob, PBVHNode *node,
674 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
675 SculptUndoNode *unode;
676 SculptSession *ss = ob->sculpt;
677 int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
679 unode = MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
680 BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
685 BKE_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
686 BKE_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
687 &maxgrid, &gridsize, NULL);
689 unode->totvert = totvert;
694 /* we will use this while sculpting, is mapalloc slow to access then? */
696 /* general TODO, fix count_alloc */
698 case SCULPT_UNDO_COORDS:
699 unode->co = MEM_mapallocN(sizeof(float) * 3 * allvert, "SculptUndoNode.co");
700 unode->no = MEM_mapallocN(sizeof(short) * 3 * allvert, "SculptUndoNode.no");
701 undo_paint_push_count_alloc(UNDO_PAINT_MESH,
704 sizeof(int)) * allvert);
706 case SCULPT_UNDO_HIDDEN:
708 sculpt_undo_alloc_and_store_hidden(ss->pbvh, unode);
710 unode->vert_hidden = BLI_BITMAP_NEW(allvert, "SculptUndoNode.vert_hidden");
713 case SCULPT_UNDO_MASK:
714 unode->mask = MEM_mapallocN(sizeof(float) * allvert, "SculptUndoNode.mask");
715 undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float) * sizeof(int)) * allvert);
717 case SCULPT_UNDO_DYNTOPO_BEGIN:
718 case SCULPT_UNDO_DYNTOPO_END:
719 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
720 BLI_assert(!"Dynamic topology should've already been handled");
724 BLI_addtail(lb, unode);
728 unode->maxgrid = maxgrid;
729 unode->totgrid = totgrid;
730 unode->gridsize = gridsize;
731 unode->grids = MEM_mapallocN(sizeof(int) * totgrid, "SculptUndoNode.grids");
735 unode->maxvert = ss->totvert;
736 unode->index = MEM_mapallocN(sizeof(int) * allvert, "SculptUndoNode.index");
739 if (ss->modifiers_active)
740 unode->orig_co = MEM_callocN(allvert * sizeof(*unode->orig_co), "undoSculpt orig_cos");
745 static void sculpt_undo_store_coords(Object *ob, SculptUndoNode *unode)
747 SculptSession *ss = ob->sculpt;
750 BKE_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
752 copy_v3_v3(unode->co[vd.i], vd.co);
753 if (vd.no) copy_v3_v3_short(unode->no[vd.i], vd.no);
754 else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
756 if (ss->modifiers_active)
757 copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
759 BKE_pbvh_vertex_iter_end;
762 static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
764 PBVH *pbvh = ob->sculpt->pbvh;
765 PBVHNode *node = unode->node;
768 /* already stored during allocation */
772 const int *vert_indices;
776 BKE_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
777 BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
778 for (i = 0; i < allvert; i++) {
779 BLI_BITMAP_SET(unode->vert_hidden, i,
780 mvert[vert_indices[i]].flag & ME_HIDE);
785 static void sculpt_undo_store_mask(Object *ob, SculptUndoNode *unode)
787 SculptSession *ss = ob->sculpt;
790 BKE_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
792 unode->mask[vd.i] = *vd.mask;
794 BKE_pbvh_vertex_iter_end;
797 static SculptUndoNode *sculpt_undo_bmesh_push(Object *ob,
801 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
802 SculptUndoNode *unode = lb->first;
803 SculptSession *ss = ob->sculpt;
807 unode = MEM_callocN(sizeof(*unode), __func__);
809 BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
811 unode->applied = true;
813 if (type == SCULPT_UNDO_DYNTOPO_END) {
814 unode->bm_entry = BM_log_entry_add(ss->bm_log);
815 BM_log_before_all_removed(ss->bm, ss->bm_log);
817 else if (type == SCULPT_UNDO_DYNTOPO_BEGIN) {
820 /* Store a copy of the mesh's current vertices, loops, and
821 * polys. A full copy like this is needed because entering
822 * dynamic-topology immediately does topological edits
823 * (converting polys to triangles) that the BMLog can't
824 * fully restore from */
825 CustomData_copy(&me->vdata, &unode->bm_enter_vdata, CD_MASK_MESH,
826 CD_DUPLICATE, me->totvert);
827 CustomData_copy(&me->edata, &unode->bm_enter_edata, CD_MASK_MESH,
828 CD_DUPLICATE, me->totedge);
829 CustomData_copy(&me->ldata, &unode->bm_enter_ldata, CD_MASK_MESH,
830 CD_DUPLICATE, me->totloop);
831 CustomData_copy(&me->pdata, &unode->bm_enter_pdata, CD_MASK_MESH,
832 CD_DUPLICATE, me->totpoly);
833 unode->bm_enter_totvert = me->totvert;
834 unode->bm_enter_totedge = me->totedge;
835 unode->bm_enter_totloop = me->totloop;
836 unode->bm_enter_totpoly = me->totpoly;
838 unode->bm_entry = BM_log_entry_add(ss->bm_log);
839 BM_log_all_added(ss->bm, ss->bm_log);
842 unode->bm_entry = BM_log_entry_add(ss->bm_log);
845 BLI_addtail(lb, unode);
850 case SCULPT_UNDO_COORDS:
851 case SCULPT_UNDO_MASK:
852 /* Before any vertex values get modified, ensure their
853 * original positions are logged */
854 BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
855 BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
857 BKE_pbvh_vertex_iter_end;
860 case SCULPT_UNDO_HIDDEN:
862 GSetIterator gs_iter;
863 GSet *faces = BKE_pbvh_bmesh_node_faces(node);
864 BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
865 BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
867 BKE_pbvh_vertex_iter_end;
869 GSET_ITER (gs_iter, faces) {
870 BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
871 BM_log_face_modified(ss->bm_log, f);
876 case SCULPT_UNDO_DYNTOPO_BEGIN:
877 case SCULPT_UNDO_DYNTOPO_END:
878 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
886 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node,
889 SculptSession *ss = ob->sculpt;
890 SculptUndoNode *unode;
892 /* list is manipulated by multiple threads, so we lock */
893 BLI_thread_lock(LOCK_CUSTOM1);
897 SCULPT_UNDO_DYNTOPO_BEGIN,
898 SCULPT_UNDO_DYNTOPO_END))
900 /* Dynamic topology stores only one undo node per stroke,
901 * regardless of the number of PBVH nodes modified */
902 unode = sculpt_undo_bmesh_push(ob, node, type);
903 BLI_thread_unlock(LOCK_CUSTOM1);
906 else if ((unode = sculpt_undo_get_node(node))) {
907 BLI_thread_unlock(LOCK_CUSTOM1);
911 unode = sculpt_undo_alloc_node(ob, node, type);
913 BLI_thread_unlock(LOCK_CUSTOM1);
915 /* copy threaded, hopefully this is the performance critical part */
919 BKE_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
921 memcpy(unode->grids, grids, sizeof(int) * totgrid);
924 const int *vert_indices;
926 BKE_pbvh_node_num_verts(ss->pbvh, node, NULL, &allvert);
927 BKE_pbvh_node_get_verts(ss->pbvh, node, &vert_indices, NULL);
928 memcpy(unode->index, vert_indices, sizeof(int) * unode->totvert);
932 case SCULPT_UNDO_COORDS:
933 sculpt_undo_store_coords(ob, unode);
935 case SCULPT_UNDO_HIDDEN:
936 sculpt_undo_store_hidden(ob, unode);
938 case SCULPT_UNDO_MASK:
939 sculpt_undo_store_mask(ob, unode);
941 case SCULPT_UNDO_DYNTOPO_BEGIN:
942 case SCULPT_UNDO_DYNTOPO_END:
943 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
944 BLI_assert(!"Dynamic topology should've already been handled");
948 /* store active shape key */
949 if (ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
950 else unode->shapeName[0] = '\0';
955 void sculpt_undo_push_begin(const char *name)
957 ED_undo_paint_push_begin(UNDO_PAINT_MESH, name,
958 sculpt_undo_restore, sculpt_undo_free, sculpt_undo_cleanup);
961 void sculpt_undo_push_end(void)
963 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
964 SculptUndoNode *unode;
966 /* we don't need normals in the undo stack */
967 for (unode = lb->first; unode; unode = unode->next) {
969 MEM_freeN(unode->no);
974 BKE_pbvh_node_layer_disp_free(unode->node);
977 ED_undo_paint_push_end(UNDO_PAINT_MESH);
979 WM_file_tag_modified();