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 Object *ob = CTX_data_active_object(C);
88 DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0);
89 SculptSession *ss = ob->sculpt;
90 SculptUndoNode *unode;
92 MultiresModifierData *mmd;
96 sculpt_update_mesh_elements(scene, ob, 0);
98 for(unode=lb->first; unode; unode=unode->next) {
99 if(!(strcmp(unode->idname, ob->id.name)==0))
103 /* regular mesh restore */
104 if(ss->totvert != unode->maxvert)
107 if (ss->kb && strcmp(ss->kb->name, unode->shapeName)) {
108 /* shape key has been changed before calling undo operator */
110 Key *key= ob_get_key(ob);
111 KeyBlock *kb= key_get_named_keyblock(key, unode->shapeName);
114 ob->shapenr= BLI_findindex(&key->block, kb) + 1;
116 sculpt_update_mesh_elements(scene, ob, 0);
117 WM_event_add_notifier(C, NC_OBJECT|ND_DATA, ob);
119 /* key has been removed -- skip this undo node */
129 vertCos= key_to_vertcos(ob, ss->kb);
131 for(i=0; i<unode->totvert; i++) {
132 if(ss->modifiers_active) sculpt_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);
148 for(i=0; i<unode->totvert; i++) {
149 if(ss->modifiers_active) sculpt_restore_deformed(ss, unode, i, index[i], mvert[index[i]].co);
151 if(unode->orig_co) swap_v3_v3(mvert[index[i]].co, unode->orig_co[i]);
152 else swap_v3_v3(mvert[index[i]].co, unode->co[i]);
154 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
158 else if(unode->maxgrid && dm->getGridData) {
159 /* multires restore */
160 DMGridData **grids, *grid;
164 if(dm->getNumGrids(dm) != unode->maxgrid)
166 if(dm->getGridSize(dm) != unode->gridsize)
169 grids= dm->getGridData(dm);
170 gridsize= dm->getGridSize(dm);
173 for(j=0; j<unode->totgrid; j++) {
174 grid= grids[unode->grids[j]];
176 for(i=0; i<gridsize*gridsize; i++, co++)
177 swap_v3_v3(grid[i].co, co[0]);
186 /* we update all nodes still, should be more clever, but also
187 needs to work correct when exiting/entering sculpt mode and
188 the nodes get recreated, though in that case it could do all */
189 BLI_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, NULL);
190 BLI_pbvh_update(ss->pbvh, PBVH_UpdateBB|PBVH_UpdateOriginalBB|PBVH_UpdateRedraw, NULL);
192 if((mmd=sculpt_multires_active(scene, ob)))
193 multires_mark_as_modified(ob);
195 tag_update= ((Mesh*)ob->data)->id.us > 1;
197 if(ss->modifiers_active) {
199 mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
201 sculpt_free_deformMats(ss);
206 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
208 /* for non-PBVH drawing, need to recreate VBOs */
209 GPU_drawobject_free(ob->derivedFinal);
213 static void sculpt_undo_free(ListBase *lb)
215 SculptUndoNode *unode;
217 for(unode=lb->first; unode; unode=unode->next) {
219 MEM_freeN(unode->co);
221 MEM_freeN(unode->no);
223 MEM_freeN(unode->index);
225 MEM_freeN(unode->grids);
226 if(unode->layer_disp)
227 MEM_freeN(unode->layer_disp);
229 MEM_freeN(unode->orig_co);
233 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
235 ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
236 SculptUndoNode *unode;
241 for(unode=lb->first; unode; unode=unode->next)
242 if(unode->node == node)
248 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node)
250 ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
251 SculptSession *ss = ob->sculpt;
252 SculptUndoNode *unode;
253 int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
255 /* list is manipulated by multiple threads, so we lock */
256 BLI_lock_thread(LOCK_CUSTOM1);
258 if((unode= sculpt_undo_get_node(node))) {
259 BLI_unlock_thread(LOCK_CUSTOM1);
263 unode= MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
264 strcpy(unode->idname, ob->id.name);
267 BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
268 BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
269 &maxgrid, &gridsize, NULL, NULL);
271 unode->totvert= totvert;
272 /* we will use this while sculpting, is mapalloc slow to access then? */
273 unode->co= MEM_mapallocN(sizeof(float)*3*allvert, "SculptUndoNode.co");
274 unode->no= MEM_mapallocN(sizeof(short)*3*allvert, "SculptUndoNode.no");
275 undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float)*3 + sizeof(short)*3 + sizeof(int))*allvert);
276 BLI_addtail(lb, unode);
280 unode->maxgrid= maxgrid;
281 unode->totgrid= totgrid;
282 unode->gridsize= gridsize;
283 unode->grids= MEM_mapallocN(sizeof(int)*totgrid, "SculptUndoNode.grids");
287 unode->maxvert= ss->totvert;
288 unode->index= MEM_mapallocN(sizeof(int)*allvert, "SculptUndoNode.index");
291 if(ss->modifiers_active)
292 unode->orig_co= MEM_callocN(allvert*sizeof(*unode->orig_co), "undoSculpt orig_cos");
294 BLI_unlock_thread(LOCK_CUSTOM1);
296 /* copy threaded, hopefully this is the performance critical part */
300 BLI_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
301 copy_v3_v3(unode->co[vd.i], vd.co);
302 if(vd.no) VECCOPY(unode->no[vd.i], vd.no)
303 else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
304 if(vd.vert_indices) unode->index[vd.i]= vd.vert_indices[vd.i];
306 if(ss->modifiers_active)
307 copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
309 BLI_pbvh_vertex_iter_end;
313 memcpy(unode->grids, grids, sizeof(int)*totgrid);
315 /* store active shape key */
316 if(ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
317 else unode->shapeName[0]= '\0';
322 void sculpt_undo_push_begin(const char *name)
324 undo_paint_push_begin(UNDO_PAINT_MESH, name,
325 sculpt_undo_restore, sculpt_undo_free);
328 void sculpt_undo_push_end(void)
330 ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
331 SculptUndoNode *unode;
333 /* we don't need normals in the undo stack */
334 for(unode=lb->first; unode; unode=unode->next) {
336 MEM_freeN(unode->no);
340 if(unode->layer_disp) {
341 MEM_freeN(unode->layer_disp);
342 unode->layer_disp= NULL;
346 undo_paint_push_end(UNDO_PAINT_MESH);