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
36 #include "MEM_guardedalloc.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_string.h"
41 #include "BLI_listbase.h"
42 #include "BLI_ghash.h"
43 #include "BLI_threads.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_mesh_types.h"
51 #include "BKE_cdderivedmesh.h"
52 #include "BKE_context.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_modifier.h"
55 #include "BKE_multires.h"
56 #include "BKE_paint.h"
59 #include "BKE_subsurf.h"
64 #include "GPU_buffers.h"
66 #include "ED_sculpt.h"
67 #include "paint_intern.h"
68 #include "sculpt_intern.h"
70 /************************** Undo *************************/
72 static void update_cb(PBVHNode *node, void *rebuild)
74 BLI_pbvh_node_mark_update(node);
75 if (*((int *)rebuild))
76 BLI_pbvh_node_mark_rebuild_draw(node);
77 BLI_pbvh_node_fully_hidden_set(node, 0);
80 static void sculpt_undo_restore_deformed(const SculptSession *ss,
81 SculptUndoNode *unode,
82 int uindex, int oindex,
86 swap_v3_v3(coord, unode->orig_co[uindex]);
87 copy_v3_v3(unode->co[uindex], ss->deform_cos[oindex]);
90 swap_v3_v3(coord, unode->co[uindex]);
94 static int sculpt_undo_restore_coords(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
96 Scene *scene = CTX_data_scene(C);
97 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
98 Object *ob = CTX_data_active_object(C);
99 SculptSession *ss = ob->sculpt;
103 if (unode->maxvert) {
104 /* regular mesh restore */
106 if (ss->kb && strcmp(ss->kb->name, unode->shapeName)) {
107 /* shape key has been changed before calling undo operator */
109 Key *key = ob_get_key(ob);
110 KeyBlock *kb = key_get_named_keyblock(key, unode->shapeName);
113 ob->shapenr = BLI_findindex(&key->block, kb) + 1;
115 sculpt_update_mesh_elements(scene, sd, ob, 0);
116 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, ob);
119 /* key has been removed -- skip this undo node */
124 index = unode->index;
129 vertCos = key_to_vertcos(ob, ss->kb);
131 for (i = 0; i < unode->totvert; i++) {
132 if (ss->modifiers_active) sculpt_undo_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
134 if (unode->orig_co) swap_v3_v3(vertCos[index[i]], unode->orig_co[i]);
135 else swap_v3_v3(vertCos[index[i]], unode->co[i]);
139 /* propagate new coords to keyblock */
140 sculpt_vertcos_to_key(ob, ss->kb, vertCos);
142 /* pbvh uses it's own mvert array, so coords should be */
143 /* propagated to pbvh here */
144 BLI_pbvh_apply_vertCos(ss->pbvh, vertCos);
149 for (i = 0; i < unode->totvert; i++) {
150 if (ss->modifiers_active) sculpt_undo_restore_deformed(ss, unode, i, index[i], mvert[index[i]].co);
152 if (unode->orig_co) swap_v3_v3(mvert[index[i]].co, unode->orig_co[i]);
153 else swap_v3_v3(mvert[index[i]].co, unode->co[i]);
155 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
159 else if (unode->maxgrid && dm->getGridData) {
160 /* multires restore */
161 CCGElem **grids, *grid;
166 grids = dm->getGridData(dm);
167 gridsize = dm->getGridSize(dm);
168 dm->getGridKey(dm, &key);
171 for (j = 0; j < unode->totgrid; j++) {
172 grid = grids[unode->grids[j]];
174 for (i = 0; i < gridsize * gridsize; i++, co++)
175 swap_v3_v3(CCG_elem_offset_co(&key, grid, i), co[0]);
182 static int sculpt_undo_restore_hidden(bContext *C, DerivedMesh *dm,
183 SculptUndoNode *unode)
185 Object *ob = CTX_data_active_object(C);
186 SculptSession *ss = ob->sculpt;
189 if (unode->maxvert) {
190 MVert *mvert = ss->mvert;
192 for (i = 0; i < unode->totvert; i++) {
193 MVert *v = &mvert[unode->index[i]];
194 int uval = BLI_BITMAP_GET(unode->vert_hidden, i);
196 BLI_BITMAP_MODIFY(unode->vert_hidden, i,
203 v->flag |= ME_VERT_PBVH_UPDATE;
206 else if (unode->maxgrid && dm->getGridData) {
207 BLI_bitmap *grid_hidden = dm->getGridHidden(dm);
209 for (i = 0; i < unode->totgrid; i++) {
211 unode->grid_hidden[i],
212 grid_hidden[unode->grids[i]]);
220 static int sculpt_undo_restore_mask(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
222 Object *ob = CTX_data_active_object(C);
223 SculptSession *ss = ob->sculpt;
228 if (unode->maxvert) {
229 /* regular mesh restore */
231 index = unode->index;
235 for (i = 0; i < unode->totvert; i++) {
236 SWAP(float, vmask[index[i]], unode->mask[i]);
237 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
240 else if (unode->maxgrid && dm->getGridData) {
241 /* multires restore */
242 CCGElem **grids, *grid;
247 grids = dm->getGridData(dm);
248 gridsize = dm->getGridSize(dm);
249 dm->getGridKey(dm, &key);
252 for (j = 0; j < unode->totgrid; j++) {
253 grid = grids[unode->grids[j]];
255 for (i = 0; i < gridsize * gridsize; i++, mask++)
256 SWAP(float, *CCG_elem_offset_mask(&key, grid, i), *mask);
263 static void sculpt_undo_restore(bContext *C, ListBase *lb)
265 Scene *scene = CTX_data_scene(C);
266 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
267 Object *ob = CTX_data_active_object(C);
268 DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0);
269 SculptSession *ss = ob->sculpt;
270 SculptUndoNode *unode;
271 MultiresModifierData *mmd;
272 int update = 0, rebuild = 1;
274 sculpt_update_mesh_elements(scene, sd, ob, 0);
276 for (unode = lb->first; unode; unode = unode->next) {
277 if (!(strcmp(unode->idname, ob->id.name) == 0))
280 /* check if undo data matches current data well enough to
282 if (unode->maxvert) {
283 if (ss->totvert != unode->maxvert)
286 else if (unode->maxgrid && dm->getGridData) {
287 if ((dm->getNumGrids(dm) != unode->maxgrid) ||
288 (dm->getGridSize(dm) != unode->gridsize))
297 switch (unode->type) {
298 case SCULPT_UNDO_COORDS:
299 if (sculpt_undo_restore_coords(C, dm, unode))
302 case SCULPT_UNDO_HIDDEN:
303 if (sculpt_undo_restore_hidden(C, dm, unode))
306 case SCULPT_UNDO_MASK:
307 if (sculpt_undo_restore_mask(C, dm, unode))
313 if (update || rebuild) {
315 /* we update all nodes still, should be more clever, but also
316 * needs to work correct when exiting/entering sculpt mode and
317 * the nodes get recreated, though in that case it could do all */
318 BLI_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
319 BLI_pbvh_update(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw, NULL);
321 if ((mmd = sculpt_multires_active(scene, ob))) {
323 multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
325 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
328 tag_update = ((Mesh *)ob->data)->id.us > 1;
330 if (ss->modifiers_active) {
331 Mesh *mesh = ob->data;
332 BKE_mesh_calc_normals_tessface(mesh->mvert, mesh->totvert,
333 mesh->mface, mesh->totface, NULL);
335 free_sculptsession_deformMats(ss);
340 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
342 /* for non-PBVH drawing, need to recreate VBOs */
343 GPU_drawobject_free(ob->derivedFinal);
347 static void sculpt_undo_free(ListBase *lb)
349 SculptUndoNode *unode;
352 for (unode = lb->first; unode; unode = unode->next) {
354 MEM_freeN(unode->co);
356 MEM_freeN(unode->no);
358 MEM_freeN(unode->index);
360 MEM_freeN(unode->grids);
361 if (unode->layer_disp)
362 MEM_freeN(unode->layer_disp);
364 MEM_freeN(unode->orig_co);
365 if (unode->vert_hidden)
366 MEM_freeN(unode->vert_hidden);
367 if (unode->grid_hidden) {
368 for (i = 0; i < unode->totgrid; i++) {
369 if (unode->grid_hidden[i])
370 MEM_freeN(unode->grid_hidden[i]);
372 MEM_freeN(unode->grid_hidden);
375 MEM_freeN(unode->mask);
379 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
381 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
382 SculptUndoNode *unode;
387 for (unode = lb->first; unode; unode = unode->next)
388 if (unode->node == node)
394 static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh,
395 SculptUndoNode *unode)
397 PBVHNode *node = unode->node;
398 BLI_bitmap *grid_hidden;
399 int i, *grid_indices, totgrid;
401 grid_hidden = BLI_pbvh_grid_hidden(pbvh);
403 BLI_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid,
404 NULL, NULL, NULL, NULL);
406 unode->grid_hidden = MEM_mapallocN(sizeof(BLI_bitmap) * totgrid,
407 "unode->grid_hidden");
409 for (i = 0; i < totgrid; i++) {
410 if (grid_hidden[grid_indices[i]])
411 unode->grid_hidden[i] = MEM_dupallocN(grid_hidden[grid_indices[i]]);
413 unode->grid_hidden[i] = NULL;
417 static SculptUndoNode *sculpt_undo_alloc_node(Object *ob, PBVHNode *node,
420 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
421 SculptUndoNode *unode;
422 SculptSession *ss = ob->sculpt;
423 int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
425 unode = MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
426 BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
430 BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
431 BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
432 &maxgrid, &gridsize, NULL, NULL);
434 unode->totvert = totvert;
436 /* we will use this while sculpting, is mapalloc slow to access then? */
438 /* general TODO, fix count_alloc */
440 case SCULPT_UNDO_COORDS:
441 unode->co = MEM_mapallocN(sizeof(float) * 3 * allvert, "SculptUndoNode.co");
442 unode->no = MEM_mapallocN(sizeof(short) * 3 * allvert, "SculptUndoNode.no");
443 undo_paint_push_count_alloc(UNDO_PAINT_MESH,
446 sizeof(int)) * allvert);
448 case SCULPT_UNDO_HIDDEN:
450 sculpt_undo_alloc_and_store_hidden(ss->pbvh, unode);
452 unode->vert_hidden = BLI_BITMAP_NEW(allvert, "SculptUndoNode.vert_hidden");
455 case SCULPT_UNDO_MASK:
456 unode->mask = MEM_mapallocN(sizeof(float) * allvert, "SculptUndoNode.mask");
457 undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float) * sizeof(int)) * allvert);
461 BLI_addtail(lb, unode);
465 unode->maxgrid = maxgrid;
466 unode->totgrid = totgrid;
467 unode->gridsize = gridsize;
468 unode->grids = MEM_mapallocN(sizeof(int) * totgrid, "SculptUndoNode.grids");
472 unode->maxvert = ss->totvert;
473 unode->index = MEM_mapallocN(sizeof(int) * allvert, "SculptUndoNode.index");
476 if (ss->modifiers_active)
477 unode->orig_co = MEM_callocN(allvert * sizeof(*unode->orig_co), "undoSculpt orig_cos");
482 static void sculpt_undo_store_coords(Object *ob, SculptUndoNode *unode)
484 SculptSession *ss = ob->sculpt;
487 BLI_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
489 copy_v3_v3(unode->co[vd.i], vd.co);
490 if (vd.no) copy_v3_v3_short(unode->no[vd.i], vd.no);
491 else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
493 if (ss->modifiers_active)
494 copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
496 BLI_pbvh_vertex_iter_end;
499 static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
501 PBVH *pbvh = ob->sculpt->pbvh;
502 PBVHNode *node = unode->node;
505 /* already stored during allocation */
509 int *vert_indices, allvert;
512 BLI_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
513 BLI_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
514 for (i = 0; i < allvert; i++) {
515 BLI_BITMAP_MODIFY(unode->vert_hidden, i,
516 mvert[vert_indices[i]].flag & ME_HIDE);
521 static void sculpt_undo_store_mask(Object *ob, SculptUndoNode *unode)
523 SculptSession *ss = ob->sculpt;
526 BLI_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
528 unode->mask[vd.i] = *vd.mask;
530 BLI_pbvh_vertex_iter_end;
533 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node,
536 SculptSession *ss = ob->sculpt;
537 SculptUndoNode *unode;
539 /* list is manipulated by multiple threads, so we lock */
540 BLI_lock_thread(LOCK_CUSTOM1);
542 if ((unode = sculpt_undo_get_node(node))) {
543 BLI_unlock_thread(LOCK_CUSTOM1);
547 unode = sculpt_undo_alloc_node(ob, node, type);
549 BLI_unlock_thread(LOCK_CUSTOM1);
551 /* copy threaded, hopefully this is the performance critical part */
555 BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
556 NULL, NULL, NULL, NULL);
557 memcpy(unode->grids, grids, sizeof(int) * totgrid);
560 int *vert_indices, allvert;
561 BLI_pbvh_node_num_verts(ss->pbvh, node, NULL, &allvert);
562 BLI_pbvh_node_get_verts(ss->pbvh, node, &vert_indices, NULL);
563 memcpy(unode->index, vert_indices, sizeof(int) * unode->totvert);
567 case SCULPT_UNDO_COORDS:
568 sculpt_undo_store_coords(ob, unode);
570 case SCULPT_UNDO_HIDDEN:
571 sculpt_undo_store_hidden(ob, unode);
573 case SCULPT_UNDO_MASK:
574 sculpt_undo_store_mask(ob, unode);
578 /* store active shape key */
579 if (ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
580 else unode->shapeName[0] = '\0';
585 void sculpt_undo_push_begin(const char *name)
587 undo_paint_push_begin(UNDO_PAINT_MESH, name,
588 sculpt_undo_restore, sculpt_undo_free);
591 void sculpt_undo_push_end(void)
593 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
594 SculptUndoNode *unode;
596 /* we don't need normals in the undo stack */
597 for (unode = lb->first; unode; unode = unode->next) {
599 MEM_freeN(unode->no);
603 if (unode->layer_disp) {
604 MEM_freeN(unode->layer_disp);
605 unode->layer_disp = NULL;
609 undo_paint_push_end(UNDO_PAINT_MESH);