svn merge ^/trunk/blender -r41100:41150
[blender.git] / source / blender / editors / sculpt_paint / sculpt_undo.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2006 by Nicholas Bishop
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  * Implements the Sculpt Mode tools
30  *
31  */
32
33 /** \file blender/editors/sculpt_paint/sculpt_undo.c
34  *  \ingroup edsculpt
35  */
36
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_math.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_string.h"
43 #include "BLI_listbase.h"
44 #include "BLI_ghash.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
52 #include "BKE_cdderivedmesh.h"
53 #include "BKE_context.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_modifier.h"
56 #include "BKE_multires.h"
57 #include "BKE_paint.h"
58 #include "BKE_key.h"
59 #include "BKE_mesh.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 #include "GPU_buffers.h"
65
66 #include "ED_sculpt.h"
67 #include "paint_intern.h"
68 #include "sculpt_intern.h"
69
70 /************************** Undo *************************/
71
72 static void update_cb(PBVHNode *node, void *unused)
73 {
74         (void)unused;
75         BLI_pbvh_node_mark_update(node);
76 }
77
78 static void sculpt_restore_deformed(SculptSession *ss, SculptUndoNode *unode, int uindex, int oindex, float coord[3])
79 {
80         if(unode->orig_co) {
81                 swap_v3_v3(coord, unode->orig_co[uindex]);
82                 copy_v3_v3(unode->co[uindex], ss->deform_cos[oindex]);
83         } else swap_v3_v3(coord, unode->co[uindex]);
84 }
85
86 static void sculpt_undo_restore(bContext *C, ListBase *lb)
87 {
88         Scene *scene = CTX_data_scene(C);
89         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
90         Object *ob = CTX_data_active_object(C);
91         DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0);
92         SculptSession *ss = ob->sculpt;
93         SculptUndoNode *unode;
94         MVert *mvert;
95         MultiresModifierData *mmd;
96         int *index;
97         int i, j, update= 0;
98
99         sculpt_update_mesh_elements(scene, sd, ob, 0);
100
101         for(unode=lb->first; unode; unode=unode->next) {
102                 if(!(strcmp(unode->idname, ob->id.name)==0))
103                         continue;
104
105                 if(unode->maxvert) {
106                         /* regular mesh restore */
107                         if(ss->totvert != unode->maxvert)
108                                 continue;
109
110                         if (ss->kb && strcmp(ss->kb->name, unode->shapeName)) {
111                                 /* shape key has been changed before calling undo operator */
112
113                                 Key *key= ob_get_key(ob);
114                                 KeyBlock *kb= key_get_named_keyblock(key, unode->shapeName);
115
116                                 if (kb) {
117                                         ob->shapenr= BLI_findindex(&key->block, kb) + 1;
118
119                                         sculpt_update_mesh_elements(scene, sd, ob, 0);
120                                         WM_event_add_notifier(C, NC_OBJECT|ND_DATA, ob);
121                                 } else {
122                                         /* key has been removed -- skip this undo node */
123                                         continue;
124                                 }
125                         }
126
127                         index= unode->index;
128                         mvert= ss->mvert;
129
130                         if (ss->kb) {
131                                 float (*vertCos)[3];
132                                 vertCos= key_to_vertcos(ob, ss->kb);
133
134                                 for(i=0; i<unode->totvert; i++) {
135                                         if(ss->modifiers_active) sculpt_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
136                                         else {
137                                                 if(unode->orig_co) swap_v3_v3(vertCos[index[i]], unode->orig_co[i]);
138                                                 else swap_v3_v3(vertCos[index[i]], unode->co[i]);
139                                         }
140                                 }
141
142                                 /* propagate new coords to keyblock */
143                                 sculpt_vertcos_to_key(ob, ss->kb, vertCos);
144
145                                 /* pbvh uses it's own mvert array, so coords should be */
146                                 /* propagated to pbvh here */
147                                 BLI_pbvh_apply_vertCos(ss->pbvh, vertCos);
148
149                                 MEM_freeN(vertCos);
150                         } else {
151                                 for(i=0; i<unode->totvert; i++) {
152                                         if(ss->modifiers_active) sculpt_restore_deformed(ss, unode, i, index[i], mvert[index[i]].co);
153                                         else {
154                                                 if(unode->orig_co) swap_v3_v3(mvert[index[i]].co, unode->orig_co[i]);
155                                                 else swap_v3_v3(mvert[index[i]].co, unode->co[i]);
156                                         }
157                                         mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
158                                 }
159                         }
160                 }
161                 else if(unode->maxgrid && dm->getGridData) {
162                         /* multires restore */
163                         DMGridData **grids, *grid;
164                         float (*co)[3];
165                         int gridsize;
166
167                         if(dm->getNumGrids(dm) != unode->maxgrid)
168                                 continue;
169                         if(dm->getGridSize(dm) != unode->gridsize)
170                                 continue;
171
172                         grids= dm->getGridData(dm);
173                         gridsize= dm->getGridSize(dm);
174
175                         co = unode->co;
176                         for(j=0; j<unode->totgrid; j++) {
177                                 grid= grids[unode->grids[j]];
178
179                                 for(i=0; i<gridsize*gridsize; i++, co++)
180                                         swap_v3_v3(grid[i].co, co[0]);
181                         }
182                 }
183
184                 update= 1;
185         }
186
187         if(update) {
188                 int tag_update= 0;
189                 /* we update all nodes still, should be more clever, but also
190                    needs to work correct when exiting/entering sculpt mode and
191                    the nodes get recreated, though in that case it could do all */
192                 BLI_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, NULL);
193                 BLI_pbvh_update(ss->pbvh, PBVH_UpdateBB|PBVH_UpdateOriginalBB|PBVH_UpdateRedraw, NULL);
194
195                 if((mmd=sculpt_multires_active(scene, ob)))
196                         multires_mark_as_modified(ob);
197
198                 tag_update= ((Mesh*)ob->data)->id.us > 1;
199
200                 if(ss->modifiers_active) {
201                         Mesh *mesh= ob->data;
202                         mesh_calc_normals(mesh->mvert, mesh->totvert, mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly, NULL, NULL, 0, NULL, NULL);
203
204                         free_sculptsession_deformMats(ss);
205                         tag_update|= 1;
206                 }
207
208                 if(tag_update)
209                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
210
211                 /* for non-PBVH drawing, need to recreate VBOs */
212                 GPU_drawobject_free(ob->derivedFinal);
213         }
214 }
215
216 static void sculpt_undo_free(ListBase *lb)
217 {
218         SculptUndoNode *unode;
219
220         for(unode=lb->first; unode; unode=unode->next) {
221                 if(unode->co)
222                         MEM_freeN(unode->co);
223                 if(unode->no)
224                         MEM_freeN(unode->no);
225                 if(unode->index)
226                         MEM_freeN(unode->index);
227                 if(unode->grids)
228                         MEM_freeN(unode->grids);
229                 if(unode->layer_disp)
230                         MEM_freeN(unode->layer_disp);
231                 if(unode->orig_co)
232                         MEM_freeN(unode->orig_co);
233         }
234 }
235
236 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
237 {
238         ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
239         SculptUndoNode *unode;
240
241         if(!lb)
242                 return NULL;
243
244         for(unode=lb->first; unode; unode=unode->next)
245                 if(unode->node == node)
246                         return unode;
247
248         return NULL;
249 }
250
251 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node)
252 {
253         ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
254         SculptSession *ss = ob->sculpt;
255         SculptUndoNode *unode;
256         int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
257
258         /* list is manipulated by multiple threads, so we lock */
259         BLI_lock_thread(LOCK_CUSTOM1);
260
261         if((unode= sculpt_undo_get_node(node))) {
262                 BLI_unlock_thread(LOCK_CUSTOM1);
263                 return unode;
264         }
265
266         unode= MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
267         BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
268         unode->node= node;
269
270         BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
271         BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
272                 &maxgrid, &gridsize, NULL, NULL);
273
274         unode->totvert= totvert;
275         /* we will use this while sculpting, is mapalloc slow to access then? */
276         unode->co= MEM_mapallocN(sizeof(float)*3*allvert, "SculptUndoNode.co");
277         unode->no= MEM_mapallocN(sizeof(short)*3*allvert, "SculptUndoNode.no");
278         undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float)*3 + sizeof(short)*3 + sizeof(int))*allvert);
279         BLI_addtail(lb, unode);
280
281         if(maxgrid) {
282                 /* multires */
283                 unode->maxgrid= maxgrid;
284                 unode->totgrid= totgrid;
285                 unode->gridsize= gridsize;
286                 unode->grids= MEM_mapallocN(sizeof(int)*totgrid, "SculptUndoNode.grids");
287         }
288         else {
289                 /* regular mesh */
290                 unode->maxvert= ss->totvert;
291                 unode->index= MEM_mapallocN(sizeof(int)*allvert, "SculptUndoNode.index");
292         }
293
294         if(ss->modifiers_active)
295                 unode->orig_co= MEM_callocN(allvert*sizeof(*unode->orig_co), "undoSculpt orig_cos");
296
297         BLI_unlock_thread(LOCK_CUSTOM1);
298
299         /* copy threaded, hopefully this is the performance critical part */
300         {
301                 PBVHVertexIter vd;
302
303                 BLI_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
304                         copy_v3_v3(unode->co[vd.i], vd.co);
305                         if(vd.no) copy_v3_v3_short(unode->no[vd.i], vd.no);
306                         else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
307                         if(vd.vert_indices) unode->index[vd.i]= vd.vert_indices[vd.i];
308
309                         if(ss->modifiers_active)
310                                 copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
311                 }
312                 BLI_pbvh_vertex_iter_end;
313         }
314
315         if(unode->grids)
316                 memcpy(unode->grids, grids, sizeof(int)*totgrid);
317
318         /* store active shape key */
319         if(ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
320         else unode->shapeName[0]= '\0';
321
322         return unode;
323 }
324
325 void sculpt_undo_push_begin(const char *name)
326 {
327         undo_paint_push_begin(UNDO_PAINT_MESH, name,
328                 sculpt_undo_restore, sculpt_undo_free);
329 }
330
331 void sculpt_undo_push_end(void)
332 {
333         ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
334         SculptUndoNode *unode;
335
336         /* we don't need normals in the undo stack */
337         for(unode=lb->first; unode; unode=unode->next) {
338                 if(unode->no) {
339                         MEM_freeN(unode->no);
340                         unode->no= NULL;
341                 }
342
343                 if(unode->layer_disp) {
344                         MEM_freeN(unode->layer_disp);
345                         unode->layer_disp= NULL;
346                 }
347         }
348
349         undo_paint_push_end(UNDO_PAINT_MESH);
350 }