4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2006 by Nicholas Bishop
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
29 * Implements the Sculpt Mode tools
33 /** \file blender/editors/sculpt_paint/sculpt_undo.c
38 #include "MEM_guardedalloc.h"
41 #include "BLI_utildefines.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"
50 #include "BKE_cdderivedmesh.h"
51 #include "BKE_context.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_modifier.h"
54 #include "BKE_multires.h"
55 #include "BKE_paint.h"
62 #include "GPU_buffers.h"
64 #include "ED_sculpt.h"
65 #include "paint_intern.h"
66 #include "sculpt_intern.h"
68 /************************** Undo *************************/
70 static void update_cb(PBVHNode *node, void *unused)
73 BLI_pbvh_node_mark_update(node);
76 static void sculpt_restore_deformed(SculptSession *ss, SculptUndoNode *unode, int uindex, int oindex, float coord[3])
79 swap_v3_v3(coord, unode->orig_co[uindex]);
80 copy_v3_v3(unode->co[uindex], ss->deform_cos[oindex]);
81 } else swap_v3_v3(coord, unode->co[uindex]);
84 static void sculpt_undo_restore(bContext *C, ListBase *lb)
86 Scene *scene = CTX_data_scene(C);
87 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
88 Object *ob = CTX_data_active_object(C);
89 DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0);
90 SculptSession *ss = ob->sculpt;
91 SculptUndoNode *unode;
93 MultiresModifierData *mmd;
97 sculpt_update_mesh_elements(scene, sd, ob, 0);
99 for(unode=lb->first; unode; unode=unode->next) {
100 if(!(strcmp(unode->idname, ob->id.name)==0))
104 /* regular mesh restore */
105 if(ss->totvert != unode->maxvert)
108 if (ss->kb && strcmp(ss->kb->name, unode->shapeName)) {
109 /* shape key has been changed before calling undo operator */
111 Key *key= ob_get_key(ob);
112 KeyBlock *kb= key_get_named_keyblock(key, unode->shapeName);
115 ob->shapenr= BLI_findindex(&key->block, kb) + 1;
117 sculpt_update_mesh_elements(scene, sd, ob, 0);
118 WM_event_add_notifier(C, NC_OBJECT|ND_DATA, ob);
120 /* key has been removed -- skip this undo node */
130 vertCos= key_to_vertcos(ob, ss->kb);
132 for(i=0; i<unode->totvert; i++) {
133 if(ss->modifiers_active) sculpt_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
135 if(unode->orig_co) swap_v3_v3(vertCos[index[i]], unode->orig_co[i]);
136 else swap_v3_v3(vertCos[index[i]], unode->co[i]);
140 /* propagate new coords to keyblock */
141 sculpt_vertcos_to_key(ob, ss->kb, vertCos);
143 /* pbvh uses it's own mvert array, so coords should be */
144 /* propagated to pbvh here */
145 BLI_pbvh_apply_vertCos(ss->pbvh, vertCos);
149 for(i=0; i<unode->totvert; i++) {
150 if(ss->modifiers_active) sculpt_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 DMGridData **grids, *grid;
165 if(dm->getNumGrids(dm) != unode->maxgrid)
167 if(dm->getGridSize(dm) != unode->gridsize)
170 grids= dm->getGridData(dm);
171 gridsize= dm->getGridSize(dm);
174 for(j=0; j<unode->totgrid; j++) {
175 grid= grids[unode->grids[j]];
177 for(i=0; i<gridsize*gridsize; i++, co++)
178 swap_v3_v3(grid[i].co, co[0]);
187 /* we update all nodes still, should be more clever, but also
188 needs to work correct when exiting/entering sculpt mode and
189 the nodes get recreated, though in that case it could do all */
190 BLI_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, NULL);
191 BLI_pbvh_update(ss->pbvh, PBVH_UpdateBB|PBVH_UpdateOriginalBB|PBVH_UpdateRedraw, NULL);
193 if((mmd=sculpt_multires_active(scene, ob)))
194 multires_mark_as_modified(ob);
196 tag_update= ((Mesh*)ob->data)->id.us > 1;
198 if(ss->modifiers_active) {
199 Mesh *mesh= ob->data;
200 mesh_calc_normals(mesh->mvert, mesh->totvert, mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly, NULL, NULL, 0, NULL, NULL);
202 sculpt_free_deformMats(ss);
207 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
209 /* for non-PBVH drawing, need to recreate VBOs */
210 GPU_drawobject_free(ob->derivedFinal);
214 static void sculpt_undo_free(ListBase *lb)
216 SculptUndoNode *unode;
218 for(unode=lb->first; unode; unode=unode->next) {
220 MEM_freeN(unode->co);
222 MEM_freeN(unode->no);
224 MEM_freeN(unode->index);
226 MEM_freeN(unode->grids);
227 if(unode->layer_disp)
228 MEM_freeN(unode->layer_disp);
230 MEM_freeN(unode->orig_co);
234 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
236 ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
237 SculptUndoNode *unode;
242 for(unode=lb->first; unode; unode=unode->next)
243 if(unode->node == node)
249 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node)
251 ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
252 SculptSession *ss = ob->sculpt;
253 SculptUndoNode *unode;
254 int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
256 /* list is manipulated by multiple threads, so we lock */
257 BLI_lock_thread(LOCK_CUSTOM1);
259 if((unode= sculpt_undo_get_node(node))) {
260 BLI_unlock_thread(LOCK_CUSTOM1);
264 unode= MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
265 strcpy(unode->idname, ob->id.name);
268 BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
269 BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
270 &maxgrid, &gridsize, NULL, NULL);
272 unode->totvert= totvert;
273 /* we will use this while sculpting, is mapalloc slow to access then? */
274 unode->co= MEM_mapallocN(sizeof(float)*3*allvert, "SculptUndoNode.co");
275 unode->no= MEM_mapallocN(sizeof(short)*3*allvert, "SculptUndoNode.no");
276 undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float)*3 + sizeof(short)*3 + sizeof(int))*allvert);
277 BLI_addtail(lb, unode);
281 unode->maxgrid= maxgrid;
282 unode->totgrid= totgrid;
283 unode->gridsize= gridsize;
284 unode->grids= MEM_mapallocN(sizeof(int)*totgrid, "SculptUndoNode.grids");
288 unode->maxvert= ss->totvert;
289 unode->index= MEM_mapallocN(sizeof(int)*allvert, "SculptUndoNode.index");
292 if(ss->modifiers_active)
293 unode->orig_co= MEM_callocN(allvert*sizeof(*unode->orig_co), "undoSculpt orig_cos");
295 BLI_unlock_thread(LOCK_CUSTOM1);
297 /* copy threaded, hopefully this is the performance critical part */
301 BLI_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
302 copy_v3_v3(unode->co[vd.i], vd.co);
303 if(vd.no) VECCOPY(unode->no[vd.i], vd.no)
304 else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
305 if(vd.vert_indices) unode->index[vd.i]= vd.vert_indices[vd.i];
307 if(ss->modifiers_active)
308 copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
310 BLI_pbvh_vertex_iter_end;
314 memcpy(unode->grids, grids, sizeof(int)*totgrid);
316 /* store active shape key */
317 if(ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
318 else unode->shapeName[0]= '\0';
323 void sculpt_undo_push_begin(const char *name)
325 undo_paint_push_begin(UNDO_PAINT_MESH, name,
326 sculpt_undo_restore, sculpt_undo_free);
329 void sculpt_undo_push_end(void)
331 ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
332 SculptUndoNode *unode;
334 /* we don't need normals in the undo stack */
335 for(unode=lb->first; unode; unode=unode->next) {
337 MEM_freeN(unode->no);
341 if(unode->layer_disp) {
342 MEM_freeN(unode->layer_disp);
343 unode->layer_disp= NULL;
347 undo_paint_push_end(UNDO_PAINT_MESH);