Fix sculpt redo failing to enable dyntopo
[blender.git] / source / blender / editors / sculpt_paint / sculpt_undo.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2006 by Nicholas Bishop
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  * Implements the Sculpt Mode tools
28  *
29  */
30
31 /** \file blender/editors/sculpt_paint/sculpt_undo.c
32  *  \ingroup edsculpt
33  */
34
35 #include <stddef.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_math.h"
40 #include "BLI_utildefines.h"
41 #include "BLI_string.h"
42 #include "BLI_listbase.h"
43 #include "BLI_ghash.h"
44 #include "BLI_task.h"
45 #include "BLI_threads.h"
46
47 #include "DNA_meshdata_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53
54 #include "BKE_ccg.h"
55 #include "BKE_context.h"
56 #include "BKE_depsgraph.h"
57 #include "BKE_multires.h"
58 #include "BKE_paint.h"
59 #include "BKE_key.h"
60 #include "BKE_mesh.h"
61 #include "BKE_subsurf.h"
62 #include "BKE_undo_system.h"
63 #include "BKE_global.h"
64 #include "BKE_main.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "GPU_buffers.h"
70
71 #include "ED_paint.h"
72 #include "ED_object.h"
73 #include "ED_sculpt.h"
74 #include "ED_undo.h"
75
76 #include "bmesh.h"
77 #include "paint_intern.h"
78 #include "sculpt_intern.h"
79
80
81 typedef struct UndoSculpt {
82         ListBase nodes;
83
84         size_t undo_size;
85 } UndoSculpt;
86
87 static UndoSculpt *sculpt_undo_get_nodes(void);
88
89 static void update_cb(PBVHNode *node, void *rebuild)
90 {
91         BKE_pbvh_node_mark_update(node);
92         if (*((bool *)rebuild))
93                 BKE_pbvh_node_mark_rebuild_draw(node);
94         BKE_pbvh_node_fully_hidden_set(node, 0);
95 }
96
97 struct PartialUpdateData {
98         PBVH *pbvh;
99         bool rebuild;
100 };
101
102 /**
103  * A version of #update_cb that tests for 'ME_VERT_PBVH_UPDATE'
104  */
105 static void update_cb_partial(PBVHNode *node, void *userdata)
106 {
107         struct PartialUpdateData *data = userdata;
108         if (BKE_pbvh_node_vert_update_check_any(data->pbvh, node)) {
109                 update_cb(node, &(data->rebuild));
110         }
111 }
112
113 static bool test_swap_v3_v3(float a[3], float b[3])
114 {
115         /* no need for float comparison here (memory is exactly equal or not) */
116         if (memcmp(a, b, sizeof(float[3])) != 0) {
117                 swap_v3_v3(a, b);
118                 return true;
119         }
120         else {
121                 return false;
122         }
123 }
124
125 static bool sculpt_undo_restore_deformed(
126         const SculptSession *ss,
127         SculptUndoNode *unode,
128         int uindex, int oindex,
129         float coord[3])
130 {
131         if (test_swap_v3_v3(coord, unode->orig_co[uindex])) {
132                 copy_v3_v3(unode->co[uindex], ss->deform_cos[oindex]);
133                 return true;
134         }
135         else {
136                 return false;
137         }
138 }
139
140 static bool sculpt_undo_restore_coords(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
141 {
142         Scene *scene = CTX_data_scene(C);
143         Object *ob = OBACT;
144         Sculpt *sd = scene->toolsettings->sculpt;
145         SculptSession *ss = ob->sculpt;
146         MVert *mvert;
147         int *index;
148
149         if (unode->maxvert) {
150                 /* regular mesh restore */
151
152                 if (ss->kb && !STREQ(ss->kb->name, unode->shapeName)) {
153                         /* shape key has been changed before calling undo operator */
154
155                         Key *key = BKE_key_from_object(ob);
156                         KeyBlock *kb = key ? BKE_keyblock_find_name(key, unode->shapeName) : NULL;
157
158                         if (kb) {
159                                 ob->shapenr = BLI_findindex(&key->block, kb) + 1;
160
161                                 BKE_sculpt_update_mesh_elements(scene, sd, ob, false, false);
162                                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, ob);
163                         }
164                         else {
165                                 /* key has been removed -- skip this undo node */
166                                 return 0;
167                         }
168                 }
169
170                 /* no need for float comparison here (memory is exactly equal or not) */
171                 index = unode->index;
172                 mvert = ss->mvert;
173
174                 if (ss->kb) {
175                         float (*vertCos)[3];
176                         vertCos = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
177
178                         if (unode->orig_co) {
179                                 if (ss->modifiers_active) {
180                                         for (int i = 0; i < unode->totvert; i++) {
181                                                 sculpt_undo_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
182                                         }
183                                 }
184                                 else {
185                                         for (int i = 0; i < unode->totvert; i++) {
186                                                 swap_v3_v3(vertCos[index[i]], unode->orig_co[i]);
187                                         }
188                                 }
189                         }
190                         else {
191                                 for (int i = 0; i < unode->totvert; i++) {
192                                         swap_v3_v3(vertCos[index[i]], unode->co[i]);
193                                 }
194                         }
195
196                         /* propagate new coords to keyblock */
197                         sculpt_vertcos_to_key(ob, ss->kb, vertCos);
198
199                         /* pbvh uses it's own mvert array, so coords should be */
200                         /* propagated to pbvh here */
201                         BKE_pbvh_apply_vertCos(ss->pbvh, vertCos);
202
203                         MEM_freeN(vertCos);
204                 }
205                 else {
206                         if (unode->orig_co) {
207                                 if (ss->modifiers_active) {
208                                         for (int i = 0; i < unode->totvert; i++) {
209                                                 if (sculpt_undo_restore_deformed(ss, unode, i, index[i], mvert[index[i]].co)) {
210                                                         mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
211                                                 }
212                                         }
213                                 }
214                                 else {
215                                         for (int i = 0; i < unode->totvert; i++) {
216                                                 if (test_swap_v3_v3(mvert[index[i]].co, unode->orig_co[i])) {
217                                                         mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
218                                                 }
219                                         }
220                                 }
221                         }
222                         else {
223                                 for (int i = 0; i < unode->totvert; i++) {
224                                         if (test_swap_v3_v3(mvert[index[i]].co, unode->co[i])) {
225                                                 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
226                                         }
227                                 }
228                         }
229                 }
230         }
231         else if (unode->maxgrid && dm->getGridData) {
232                 /* multires restore */
233                 CCGElem **grids, *grid;
234                 CCGKey key;
235                 float (*co)[3];
236                 int gridsize;
237
238                 grids = dm->getGridData(dm);
239                 gridsize = dm->getGridSize(dm);
240                 dm->getGridKey(dm, &key);
241
242                 co = unode->co;
243                 for (int j = 0; j < unode->totgrid; j++) {
244                         grid = grids[unode->grids[j]];
245
246                         for (int i = 0; i < gridsize * gridsize; i++, co++) {
247                                 swap_v3_v3(CCG_elem_offset_co(&key, grid, i), co[0]);
248                         }
249                 }
250         }
251
252         return 1;
253 }
254
255 static bool sculpt_undo_restore_hidden(
256         bContext *C, DerivedMesh *dm,
257         SculptUndoNode *unode)
258 {
259         Scene *scene = CTX_data_scene(C);
260         Object *ob = OBACT;
261         SculptSession *ss = ob->sculpt;
262         int i;
263
264         if (unode->maxvert) {
265                 MVert *mvert = ss->mvert;
266
267                 for (i = 0; i < unode->totvert; i++) {
268                         MVert *v = &mvert[unode->index[i]];
269                         if ((BLI_BITMAP_TEST(unode->vert_hidden, i) != 0) != ((v->flag & ME_HIDE) != 0)) {
270                                 BLI_BITMAP_FLIP(unode->vert_hidden, i);
271                                 v->flag ^= ME_HIDE;
272                                 v->flag |= ME_VERT_PBVH_UPDATE;
273                         }
274                 }
275         }
276         else if (unode->maxgrid && dm->getGridData) {
277                 BLI_bitmap **grid_hidden = dm->getGridHidden(dm);
278
279                 for (i = 0; i < unode->totgrid; i++) {
280                         SWAP(BLI_bitmap *,
281                              unode->grid_hidden[i],
282                              grid_hidden[unode->grids[i]]);
283
284                 }
285         }
286
287         return 1;
288 }
289
290 static bool sculpt_undo_restore_mask(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
291 {
292         Scene *scene = CTX_data_scene(C);
293         Object *ob = OBACT;
294         SculptSession *ss = ob->sculpt;
295         MVert *mvert;
296         float *vmask;
297         int *index, i, j;
298
299         if (unode->maxvert) {
300                 /* regular mesh restore */
301
302                 index = unode->index;
303                 mvert = ss->mvert;
304                 vmask = ss->vmask;
305
306                 for (i = 0; i < unode->totvert; i++) {
307                         if (vmask[index[i]] != unode->mask[i]) {
308                                 SWAP(float, vmask[index[i]], unode->mask[i]);
309                                 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
310                         }
311                 }
312         }
313         else if (unode->maxgrid && dm->getGridData) {
314                 /* multires restore */
315                 CCGElem **grids, *grid;
316                 CCGKey key;
317                 float *mask;
318                 int gridsize;
319
320                 grids = dm->getGridData(dm);
321                 gridsize = dm->getGridSize(dm);
322                 dm->getGridKey(dm, &key);
323
324                 mask = unode->mask;
325                 for (j = 0; j < unode->totgrid; j++) {
326                         grid = grids[unode->grids[j]];
327
328                         for (i = 0; i < gridsize * gridsize; i++, mask++)
329                                 SWAP(float, *CCG_elem_offset_mask(&key, grid, i), *mask);
330                 }
331         }
332
333         return 1;
334 }
335
336 static void sculpt_undo_bmesh_restore_generic_task_cb(
337         void *__restrict userdata,
338         const int n,
339         const ParallelRangeTLS *__restrict UNUSED(tls))
340 {
341         PBVHNode **nodes = userdata;
342
343         BKE_pbvh_node_mark_redraw(nodes[n]);
344 }
345
346 static void sculpt_undo_bmesh_restore_generic(bContext *C,
347                                               SculptUndoNode *unode,
348                                               Object *ob,
349                                               SculptSession *ss)
350 {
351         if (unode->applied) {
352                 BM_log_undo(ss->bm, ss->bm_log);
353                 unode->applied = false;
354         }
355         else {
356                 BM_log_redo(ss->bm, ss->bm_log);
357                 unode->applied = true;
358         }
359
360         if (unode->type == SCULPT_UNDO_MASK) {
361                 int totnode;
362                 PBVHNode **nodes;
363                 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
364
365                 BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
366
367                 ParallelRangeSettings settings;
368                 BLI_parallel_range_settings_defaults(&settings);
369                 settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
370                 BLI_task_parallel_range(
371                             0, totnode,
372                             nodes,
373                             sculpt_undo_bmesh_restore_generic_task_cb,
374                             &settings);
375
376                 if (nodes)
377                         MEM_freeN(nodes);
378         }
379         else {
380                 sculpt_pbvh_clear(ob);
381         }
382 }
383
384 /* Create empty sculpt BMesh and enable logging */
385 static void sculpt_undo_bmesh_enable(Object *ob,
386                                      SculptUndoNode *unode)
387 {
388         SculptSession *ss = ob->sculpt;
389         Mesh *me = ob->data;
390
391         sculpt_pbvh_clear(ob);
392
393         /* Create empty BMesh and enable logging */
394         ss->bm = BM_mesh_create(
395                 &bm_mesh_allocsize_default,
396                 &((struct BMeshCreateParams){.use_toolflags = false,}));
397         BM_data_layer_add(ss->bm, &ss->bm->vdata, CD_PAINT_MASK);
398         sculpt_dyntopo_node_layers_add(ss);
399         me->flag |= ME_SCULPT_DYNAMIC_TOPOLOGY;
400
401         /* Restore the BMLog using saved entries */
402         ss->bm_log = BM_log_from_existing_entries_create(ss->bm,
403                                                          unode->bm_entry);
404 }
405
406 static void sculpt_undo_bmesh_restore_begin(bContext *C,
407                                             SculptUndoNode *unode,
408                                             Object *ob,
409                                             SculptSession *ss)
410 {
411         if (unode->applied) {
412                 sculpt_dynamic_topology_disable(C, unode);
413                 unode->applied = false;
414         }
415         else {
416                 sculpt_undo_bmesh_enable(ob, unode);
417
418                 /* Restore the mesh from the first log entry */
419                 BM_log_redo(ss->bm, ss->bm_log);
420
421                 unode->applied = true;
422         }
423 }
424
425 static void sculpt_undo_bmesh_restore_end(bContext *C,
426                                           SculptUndoNode *unode,
427                                           Object *ob,
428                                           SculptSession *ss)
429 {
430         if (unode->applied) {
431                 sculpt_undo_bmesh_enable(ob, unode);
432
433                 /* Restore the mesh from the last log entry */
434                 BM_log_undo(ss->bm, ss->bm_log);
435
436                 unode->applied = false;
437         }
438         else {
439                 /* Disable dynamic topology sculpting */
440                 sculpt_dynamic_topology_disable(C, NULL);
441                 unode->applied = true;
442         }
443 }
444
445 /* Handle all dynamic-topology updates
446  *
447  * Returns true if this was a dynamic-topology undo step, otherwise
448  * returns false to indicate the non-dyntopo code should run. */
449 static int sculpt_undo_bmesh_restore(bContext *C,
450                                      SculptUndoNode *unode,
451                                      Object *ob,
452                                      SculptSession *ss)
453 {
454         switch (unode->type) {
455                 case SCULPT_UNDO_DYNTOPO_BEGIN:
456                         sculpt_undo_bmesh_restore_begin(C, unode, ob, ss);
457                         return true;
458
459                 case SCULPT_UNDO_DYNTOPO_END:
460                         sculpt_undo_bmesh_restore_end(C, unode, ob, ss);
461                         return true;
462
463                 default:
464                         if (ss->bm_log) {
465                                 sculpt_undo_bmesh_restore_generic(C, unode, ob, ss);
466                                 return true;
467                         }
468                         break;
469         }
470
471         return false;
472 }
473
474 static void sculpt_undo_restore_list(bContext *C, ListBase *lb)
475 {
476         Scene *scene = CTX_data_scene(C);
477         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
478         Object *ob = OBACT;
479         DerivedMesh *dm;
480         SculptSession *ss = ob->sculpt;
481         SculptUndoNode *unode;
482         bool update = false, rebuild = false;
483         bool need_mask = false;
484         bool partial_update = true;
485
486         for (unode = lb->first; unode; unode = unode->next) {
487                 if (STREQ(unode->idname, ob->id.name)) {
488                         if (unode->type == SCULPT_UNDO_MASK) {
489                                 /* is possible that we can't do the mask undo (below)
490                                  * because of the vertex count */
491                                 need_mask = true;
492                                 break;
493                         }
494                 }
495         }
496
497         BKE_sculpt_update_mesh_elements(scene, sd, ob, false, need_mask);
498
499         /* call _after_ sculpt_update_mesh_elements() which may update 'ob->derivedFinal' */
500         dm = mesh_get_derived_final(scene, ob, 0);
501
502         if (lb->first && sculpt_undo_bmesh_restore(C, lb->first, ob, ss))
503                 return;
504
505         for (unode = lb->first; unode; unode = unode->next) {
506                 if (!STREQ(unode->idname, ob->id.name))
507                         continue;
508
509                 /* check if undo data matches current data well enough to
510                  * continue */
511                 if (unode->maxvert) {
512                         if (ss->totvert != unode->maxvert)
513                                 continue;
514                 }
515                 else if (unode->maxgrid && dm->getGridData) {
516                         if ((dm->getNumGrids(dm) != unode->maxgrid) ||
517                             (dm->getGridSize(dm) != unode->gridsize))
518                         {
519                                 continue;
520                         }
521
522                         /* multi-res can't do partial updates since it doesn't flag edited vertices */
523                         partial_update = false;
524                 }
525
526                 switch (unode->type) {
527                         case SCULPT_UNDO_COORDS:
528                                 if (sculpt_undo_restore_coords(C, dm, unode))
529                                         update = true;
530                                 break;
531                         case SCULPT_UNDO_HIDDEN:
532                                 if (sculpt_undo_restore_hidden(C, dm, unode))
533                                         rebuild = true;
534                                 break;
535                         case SCULPT_UNDO_MASK:
536                                 if (sculpt_undo_restore_mask(C, dm, unode))
537                                         update = true;
538                                 break;
539
540                         case SCULPT_UNDO_DYNTOPO_BEGIN:
541                         case SCULPT_UNDO_DYNTOPO_END:
542                         case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
543                                 BLI_assert(!"Dynamic topology should've already been handled");
544                                 break;
545                 }
546         }
547
548         if (update || rebuild) {
549                 bool tag_update = false;
550                 /* we update all nodes still, should be more clever, but also
551                  * needs to work correct when exiting/entering sculpt mode and
552                  * the nodes get recreated, though in that case it could do all */
553                 if (partial_update) {
554                         struct PartialUpdateData data = {
555                                 .rebuild = rebuild,
556                                 .pbvh = ss->pbvh,
557                         };
558                         BKE_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb_partial, &data);
559                 }
560                 else {
561                         BKE_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
562                 }
563                 BKE_pbvh_update(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw, NULL);
564
565                 if (BKE_sculpt_multires_active(scene, ob)) {
566                         if (rebuild)
567                                 multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
568                         else
569                                 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
570                 }
571
572                 tag_update |= ((Mesh *)ob->data)->id.us > 1;
573
574                 if (ss->kb || ss->modifiers_active) {
575                         Mesh *mesh = ob->data;
576                         BKE_mesh_calc_normals(mesh);
577
578                         BKE_sculptsession_free_deformMats(ss);
579                         tag_update |= true;
580                 }
581
582                 if (tag_update) {
583                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
584                 }
585                 else {
586                         sculpt_update_object_bounding_box(ob);
587                 }
588
589                 /* for non-PBVH drawing, need to recreate VBOs */
590                 GPU_drawobject_free(ob->derivedFinal);
591         }
592 }
593
594 static void sculpt_undo_free_list(ListBase *lb)
595 {
596         SculptUndoNode *unode = lb->first;
597         while (unode != NULL) {
598                 SculptUndoNode *unode_next = unode->next;
599                 if (unode->co)
600                         MEM_freeN(unode->co);
601                 if (unode->no)
602                         MEM_freeN(unode->no);
603                 if (unode->index)
604                         MEM_freeN(unode->index);
605                 if (unode->grids)
606                         MEM_freeN(unode->grids);
607                 if (unode->orig_co)
608                         MEM_freeN(unode->orig_co);
609                 if (unode->vert_hidden)
610                         MEM_freeN(unode->vert_hidden);
611                 if (unode->grid_hidden) {
612                         for (int i = 0; i < unode->totgrid; i++) {
613                                 if (unode->grid_hidden[i])
614                                         MEM_freeN(unode->grid_hidden[i]);
615                         }
616                         MEM_freeN(unode->grid_hidden);
617                 }
618                 if (unode->mask)
619                         MEM_freeN(unode->mask);
620
621                 if (unode->bm_entry) {
622                         BM_log_entry_drop(unode->bm_entry);
623                 }
624
625                 if (unode->bm_enter_totvert)
626                         CustomData_free(&unode->bm_enter_vdata, unode->bm_enter_totvert);
627                 if (unode->bm_enter_totedge)
628                         CustomData_free(&unode->bm_enter_edata, unode->bm_enter_totedge);
629                 if (unode->bm_enter_totloop)
630                         CustomData_free(&unode->bm_enter_ldata, unode->bm_enter_totloop);
631                 if (unode->bm_enter_totpoly)
632                         CustomData_free(&unode->bm_enter_pdata, unode->bm_enter_totpoly);
633
634                 MEM_freeN(unode);
635
636                 unode = unode_next;
637         }
638 }
639
640 /* Most likely we don't need this. */
641 #if 0
642 static bool sculpt_undo_cleanup(bContext *C, ListBase *lb)
643 {
644         Scene *scene = CTX_data_scene(C);
645         Object *ob = OBACT;
646         SculptUndoNode *unode;
647
648         unode = lb->first;
649
650         if (unode && !STREQ(unode->idname, ob->id.name)) {
651                 if (unode->bm_entry)
652                         BM_log_cleanup_entry(unode->bm_entry);
653
654                 return true;
655         }
656
657         return false;
658 }
659 #endif
660
661 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
662 {
663         UndoSculpt *usculpt = sculpt_undo_get_nodes();
664
665         if (usculpt == NULL) {
666                 return NULL;
667         }
668
669         return BLI_findptr(&usculpt->nodes, node, offsetof(SculptUndoNode, node));
670 }
671
672 static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh,
673                                                SculptUndoNode *unode)
674 {
675         PBVHNode *node = unode->node;
676         BLI_bitmap **grid_hidden;
677         int i, *grid_indices, totgrid;
678
679         grid_hidden = BKE_pbvh_grid_hidden(pbvh);
680
681         BKE_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid,
682                                 NULL, NULL, NULL);
683
684         unode->grid_hidden = MEM_mapallocN(sizeof(*unode->grid_hidden) * totgrid,
685                                            "unode->grid_hidden");
686
687         for (i = 0; i < totgrid; i++) {
688                 if (grid_hidden[grid_indices[i]])
689                         unode->grid_hidden[i] = MEM_dupallocN(grid_hidden[grid_indices[i]]);
690                 else
691                         unode->grid_hidden[i] = NULL;
692         }
693 }
694
695 static SculptUndoNode *sculpt_undo_alloc_node(
696         Object *ob, PBVHNode *node,
697         SculptUndoType type)
698 {
699         UndoSculpt *usculpt = sculpt_undo_get_nodes();
700         SculptUndoNode *unode;
701         SculptSession *ss = ob->sculpt;
702         int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
703
704         unode = MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
705         BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
706         unode->type = type;
707         unode->node = node;
708
709         if (node) {
710                 BKE_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
711                 BKE_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
712                                         &maxgrid, &gridsize, NULL);
713
714                 unode->totvert = totvert;
715         }
716         else
717                 maxgrid = 0;
718
719         /* we will use this while sculpting, is mapalloc slow to access then? */
720
721         /* general TODO, fix count_alloc */
722         switch (type) {
723                 case SCULPT_UNDO_COORDS:
724                         unode->co = MEM_mapallocN(sizeof(float[3]) * allvert, "SculptUndoNode.co");
725                         unode->no = MEM_mapallocN(sizeof(short[3]) * allvert, "SculptUndoNode.no");
726
727                         usculpt->undo_size = (sizeof(float[3]) + sizeof(short[3]) + sizeof(int)) * allvert;
728                         break;
729                 case SCULPT_UNDO_HIDDEN:
730                         if (maxgrid)
731                                 sculpt_undo_alloc_and_store_hidden(ss->pbvh, unode);
732                         else
733                                 unode->vert_hidden = BLI_BITMAP_NEW(allvert, "SculptUndoNode.vert_hidden");
734
735                         break;
736                 case SCULPT_UNDO_MASK:
737                         unode->mask = MEM_mapallocN(sizeof(float) * allvert, "SculptUndoNode.mask");
738
739                         usculpt->undo_size += (sizeof(float) * sizeof(int)) * allvert;
740
741                         break;
742                 case SCULPT_UNDO_DYNTOPO_BEGIN:
743                 case SCULPT_UNDO_DYNTOPO_END:
744                 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
745                         BLI_assert(!"Dynamic topology should've already been handled");
746                         break;
747         }
748
749         BLI_addtail(&usculpt->nodes, unode);
750
751         if (maxgrid) {
752                 /* multires */
753                 unode->maxgrid = maxgrid;
754                 unode->totgrid = totgrid;
755                 unode->gridsize = gridsize;
756                 unode->grids = MEM_mapallocN(sizeof(int) * totgrid, "SculptUndoNode.grids");
757         }
758         else {
759                 /* regular mesh */
760                 unode->maxvert = ss->totvert;
761                 unode->index = MEM_mapallocN(sizeof(int) * allvert, "SculptUndoNode.index");
762         }
763
764         if (ss->modifiers_active)
765                 unode->orig_co = MEM_callocN(allvert * sizeof(*unode->orig_co), "undoSculpt orig_cos");
766
767         return unode;
768 }
769
770 static void sculpt_undo_store_coords(Object *ob, SculptUndoNode *unode)
771 {
772         SculptSession *ss = ob->sculpt;
773         PBVHVertexIter vd;
774
775         BKE_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
776         {
777                 copy_v3_v3(unode->co[vd.i], vd.co);
778                 if (vd.no) copy_v3_v3_short(unode->no[vd.i], vd.no);
779                 else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
780
781                 if (ss->modifiers_active)
782                         copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
783         }
784         BKE_pbvh_vertex_iter_end;
785 }
786
787 static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
788 {
789         PBVH *pbvh = ob->sculpt->pbvh;
790         PBVHNode *node = unode->node;
791
792         if (unode->grids) {
793                 /* already stored during allocation */
794         }
795         else {
796                 MVert *mvert;
797                 const int *vert_indices;
798                 int allvert;
799                 int i;
800
801                 BKE_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
802                 BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
803                 for (i = 0; i < allvert; i++) {
804                         BLI_BITMAP_SET(unode->vert_hidden, i,
805                                           mvert[vert_indices[i]].flag & ME_HIDE);
806                 }
807         }
808 }
809
810 static void sculpt_undo_store_mask(Object *ob, SculptUndoNode *unode)
811 {
812         SculptSession *ss = ob->sculpt;
813         PBVHVertexIter vd;
814
815         BKE_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
816         {
817                 unode->mask[vd.i] = *vd.mask;
818         }
819         BKE_pbvh_vertex_iter_end;
820 }
821
822 static SculptUndoNode *sculpt_undo_bmesh_push(Object *ob,
823                                               PBVHNode *node,
824                                               SculptUndoType type)
825 {
826         UndoSculpt *usculpt = sculpt_undo_get_nodes();
827         SculptSession *ss = ob->sculpt;
828         PBVHVertexIter vd;
829
830         SculptUndoNode *unode = usculpt->nodes.first;
831
832         if (unode == NULL) {
833                 unode = MEM_callocN(sizeof(*unode), __func__);
834
835                 BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
836                 unode->type = type;
837                 unode->applied = true;
838
839                 if (type == SCULPT_UNDO_DYNTOPO_END) {
840                         unode->bm_entry = BM_log_entry_add(ss->bm_log);
841                         BM_log_before_all_removed(ss->bm, ss->bm_log);
842                 }
843                 else if (type == SCULPT_UNDO_DYNTOPO_BEGIN) {
844                         Mesh *me = ob->data;
845
846                         /* Store a copy of the mesh's current vertices, loops, and
847                          * polys. A full copy like this is needed because entering
848                          * dynamic-topology immediately does topological edits
849                          * (converting polys to triangles) that the BMLog can't
850                          * fully restore from */
851                         CustomData_copy(&me->vdata, &unode->bm_enter_vdata, CD_MASK_MESH,
852                                         CD_DUPLICATE, me->totvert);
853                         CustomData_copy(&me->edata, &unode->bm_enter_edata, CD_MASK_MESH,
854                                         CD_DUPLICATE, me->totedge);
855                         CustomData_copy(&me->ldata, &unode->bm_enter_ldata, CD_MASK_MESH,
856                                         CD_DUPLICATE, me->totloop);
857                         CustomData_copy(&me->pdata, &unode->bm_enter_pdata, CD_MASK_MESH,
858                                         CD_DUPLICATE, me->totpoly);
859                         unode->bm_enter_totvert = me->totvert;
860                         unode->bm_enter_totedge = me->totedge;
861                         unode->bm_enter_totloop = me->totloop;
862                         unode->bm_enter_totpoly = me->totpoly;
863
864                         unode->bm_entry = BM_log_entry_add(ss->bm_log);
865                         BM_log_all_added(ss->bm, ss->bm_log);
866                 }
867                 else {
868                         unode->bm_entry = BM_log_entry_add(ss->bm_log);
869                 }
870
871                 BLI_addtail(&usculpt->nodes, unode);
872         }
873
874         if (node) {
875                 switch (type) {
876                         case SCULPT_UNDO_COORDS:
877                         case SCULPT_UNDO_MASK:
878                                 /* Before any vertex values get modified, ensure their
879                                  * original positions are logged */
880                                 BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
881                                         BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
882                                 }
883                                 BKE_pbvh_vertex_iter_end;
884                                 break;
885
886                         case SCULPT_UNDO_HIDDEN:
887                         {
888                                 GSetIterator gs_iter;
889                                 GSet *faces = BKE_pbvh_bmesh_node_faces(node);
890                                 BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
891                                         BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
892                                 }
893                                 BKE_pbvh_vertex_iter_end;
894
895                                 GSET_ITER (gs_iter, faces) {
896                                         BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
897                                         BM_log_face_modified(ss->bm_log, f);
898                                 }
899                                 break;
900                         }
901
902                         case SCULPT_UNDO_DYNTOPO_BEGIN:
903                         case SCULPT_UNDO_DYNTOPO_END:
904                         case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
905                                 break;
906                 }
907         }
908
909         return unode;
910 }
911
912 SculptUndoNode *sculpt_undo_push_node(
913         Object *ob, PBVHNode *node,
914         SculptUndoType type)
915 {
916         SculptSession *ss = ob->sculpt;
917         SculptUndoNode *unode;
918
919         /* list is manipulated by multiple threads, so we lock */
920         BLI_thread_lock(LOCK_CUSTOM1);
921
922         if (ss->bm ||
923             ELEM(type,
924                  SCULPT_UNDO_DYNTOPO_BEGIN,
925                  SCULPT_UNDO_DYNTOPO_END))
926         {
927                 /* Dynamic topology stores only one undo node per stroke,
928                  * regardless of the number of PBVH nodes modified */
929                 unode = sculpt_undo_bmesh_push(ob, node, type);
930                 BLI_thread_unlock(LOCK_CUSTOM1);
931                 return unode;
932         }
933         else if ((unode = sculpt_undo_get_node(node))) {
934                 BLI_thread_unlock(LOCK_CUSTOM1);
935                 return unode;
936         }
937
938         unode = sculpt_undo_alloc_node(ob, node, type);
939
940         BLI_thread_unlock(LOCK_CUSTOM1);
941
942         /* copy threaded, hopefully this is the performance critical part */
943
944         if (unode->grids) {
945                 int totgrid, *grids;
946                 BKE_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
947                                         NULL, NULL, NULL);
948                 memcpy(unode->grids, grids, sizeof(int) * totgrid);
949         }
950         else {
951                 const int *vert_indices;
952                 int allvert;
953                 BKE_pbvh_node_num_verts(ss->pbvh, node, NULL, &allvert);
954                 BKE_pbvh_node_get_verts(ss->pbvh, node, &vert_indices, NULL);
955                 memcpy(unode->index, vert_indices, sizeof(int) * unode->totvert);
956         }
957
958         switch (type) {
959                 case SCULPT_UNDO_COORDS:
960                         sculpt_undo_store_coords(ob, unode);
961                         break;
962                 case SCULPT_UNDO_HIDDEN:
963                         sculpt_undo_store_hidden(ob, unode);
964                         break;
965                 case SCULPT_UNDO_MASK:
966                         sculpt_undo_store_mask(ob, unode);
967                         break;
968                 case SCULPT_UNDO_DYNTOPO_BEGIN:
969                 case SCULPT_UNDO_DYNTOPO_END:
970                 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
971                         BLI_assert(!"Dynamic topology should've already been handled");
972                         break;
973         }
974
975         /* store active shape key */
976         if (ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
977         else unode->shapeName[0] = '\0';
978
979         return unode;
980 }
981
982 void sculpt_undo_push_begin(const char *name)
983 {
984         UndoStack *ustack = ED_undo_stack_get();
985         bContext *C = NULL; /* special case, we never read from this. */
986         BKE_undosys_step_push_init_with_type(ustack, C, name, BKE_UNDOSYS_TYPE_SCULPT);
987 }
988
989 void sculpt_undo_push_end(void)
990 {
991         UndoSculpt *usculpt = sculpt_undo_get_nodes();
992         SculptUndoNode *unode;
993
994         /* we don't need normals in the undo stack */
995         for (unode = usculpt->nodes.first; unode; unode = unode->next) {
996                 if (unode->no) {
997                         MEM_freeN(unode->no);
998                         unode->no = NULL;
999                 }
1000
1001                 if (unode->node)
1002                         BKE_pbvh_node_layer_disp_free(unode->node);
1003         }
1004
1005         /* We could remove this and enforce all callers run in an operator using 'OPTYPE_UNDO'. */
1006         wmWindowManager *wm = G_MAIN->wm.first;
1007         if (wm->op_undo_depth == 0) {
1008                 UndoStack *ustack = ED_undo_stack_get();
1009                 BKE_undosys_step_push(ustack, NULL, NULL);
1010         }
1011 }
1012
1013 /* -------------------------------------------------------------------- */
1014 /** \name Implements ED Undo System
1015  * \{ */
1016
1017 typedef struct SculptUndoStep {
1018         UndoStep step;
1019         /* note: will split out into list for multi-object-sculpt-mode. */
1020         UndoSculpt data;
1021 } SculptUndoStep;
1022
1023 static bool sculpt_undosys_poll(bContext *C)
1024 {
1025         ScrArea *sa = CTX_wm_area(C);
1026         if (sa && (sa->spacetype == SPACE_VIEW3D)) {
1027                 Scene *scene = CTX_data_scene(C);
1028                 Object *obact = OBACT;
1029                 if (obact && (obact->mode & OB_MODE_SCULPT)) {
1030                         return true;
1031                 }
1032         }
1033         return false;
1034 }
1035
1036 static void sculpt_undosys_step_encode_init(struct bContext *UNUSED(C), UndoStep *us_p)
1037 {
1038         SculptUndoStep *us = (SculptUndoStep *)us_p;
1039         /* dummy, memory is cleared anyway. */
1040         BLI_listbase_clear(&us->data.nodes);
1041 }
1042
1043 static bool sculpt_undosys_step_encode(struct bContext *UNUSED(C), UndoStep *us_p)
1044 {
1045         /* dummy, encoding is done along the way by adding tiles
1046          * to the current 'SculptUndoStep' added by encode_init. */
1047         SculptUndoStep *us = (SculptUndoStep *)us_p;
1048         us->step.data_size = us->data.undo_size;
1049         return true;
1050 }
1051
1052 static void sculpt_undosys_step_decode(struct bContext *C, UndoStep *us_p, int UNUSED(dir))
1053 {
1054         /* TODO(campbell): undo_system: use low-level API to set mode. */
1055         ED_object_mode_set(C, OB_MODE_SCULPT);
1056         BLI_assert(sculpt_undosys_poll(C));
1057
1058         SculptUndoStep *us = (SculptUndoStep *)us_p;
1059         sculpt_undo_restore_list(C, &us->data.nodes);
1060 }
1061
1062 static void sculpt_undosys_step_free(UndoStep *us_p)
1063 {
1064         SculptUndoStep *us = (SculptUndoStep *)us_p;
1065         sculpt_undo_free_list(&us->data.nodes);
1066 }
1067
1068 /* Export for ED_undo_sys. */
1069 void ED_sculpt_undosys_type(UndoType *ut)
1070 {
1071         ut->name = "Sculpt";
1072         ut->poll = sculpt_undosys_poll;
1073         ut->step_encode_init = sculpt_undosys_step_encode_init;
1074         ut->step_encode = sculpt_undosys_step_encode;
1075         ut->step_decode = sculpt_undosys_step_decode;
1076         ut->step_free = sculpt_undosys_step_free;
1077
1078         ut->mode = BKE_UNDOTYPE_MODE_ACCUMULATE;
1079         ut->use_context = true;
1080
1081         ut->step_size = sizeof(SculptUndoStep);
1082 }
1083
1084 /** \} */
1085
1086 /* -------------------------------------------------------------------- */
1087 /** \name Utilities
1088  * \{ */
1089
1090 static UndoSculpt *sculpt_undosys_step_get_nodes(UndoStep *us_p)
1091 {
1092         SculptUndoStep *us = (SculptUndoStep *)us_p;
1093         return &us->data;
1094 }
1095
1096 static UndoSculpt *sculpt_undo_get_nodes(void)
1097 {
1098         UndoStack *ustack = ED_undo_stack_get();
1099         UndoStep *us = BKE_undosys_stack_init_or_active_with_type(ustack, BKE_UNDOSYS_TYPE_SCULPT);
1100         return sculpt_undosys_step_get_nodes(us);
1101 }
1102
1103 /** \} */