svn merge -r36890:36900 https://svn.blender.org/svnroot/bf-blender/trunk/blender...
[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_ghash.h"
43 #include "BLI_threads.h"
44
45 #include "DNA_meshdata_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_mesh_types.h"
49
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"
56 #include "BKE_key.h"
57 #include "BKE_mesh.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "GPU_buffers.h"
63
64 #include "ED_sculpt.h"
65 #include "paint_intern.h"
66 #include "sculpt_intern.h"
67
68 /************************** Undo *************************/
69
70 static void update_cb(PBVHNode *node, void *unused)
71 {
72         (void)unused;
73         BLI_pbvh_node_mark_update(node);
74 }
75
76 static void sculpt_restore_deformed(SculptSession *ss, SculptUndoNode *unode, int uindex, int oindex, float coord[3])
77 {
78         if(unode->orig_co) {
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]);
82 }
83
84 static void sculpt_undo_restore(bContext *C, ListBase *lb)
85 {
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;
92         MVert *mvert;
93         MultiresModifierData *mmd;
94         int *index;
95         int i, j, update= 0;
96
97         sculpt_update_mesh_elements(scene, sd, ob, 0);
98
99         for(unode=lb->first; unode; unode=unode->next) {
100                 if(!(strcmp(unode->idname, ob->id.name)==0))
101                         continue;
102
103                 if(unode->maxvert) {
104                         /* regular mesh restore */
105                         if(ss->totvert != unode->maxvert)
106                                 continue;
107
108                         if (ss->kb && strcmp(ss->kb->name, unode->shapeName)) {
109                                 /* shape key has been changed before calling undo operator */
110
111                                 Key *key= ob_get_key(ob);
112                                 KeyBlock *kb= key_get_named_keyblock(key, unode->shapeName);
113
114                                 if (kb) {
115                                         ob->shapenr= BLI_findindex(&key->block, kb) + 1;
116
117                                         sculpt_update_mesh_elements(scene, sd, ob, 0);
118                                         WM_event_add_notifier(C, NC_OBJECT|ND_DATA, ob);
119                                 } else {
120                                         /* key has been removed -- skip this undo node */
121                                         continue;
122                                 }
123                         }
124
125                         index= unode->index;
126                         mvert= ss->mvert;
127
128                         if (ss->kb) {
129                                 float (*vertCos)[3];
130                                 vertCos= key_to_vertcos(ob, ss->kb);
131
132                                 for(i=0; i<unode->totvert; i++) {
133                                         if(ss->modifiers_active) sculpt_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
134                                         else {
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]);
137                                         }
138                                 }
139
140                                 /* propagate new coords to keyblock */
141                                 sculpt_vertcos_to_key(ob, ss->kb, vertCos);
142
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);
146
147                                 MEM_freeN(vertCos);
148                         } else {
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);
151                                         else {
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]);
154                                         }
155                                         mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
156                                 }
157                         }
158                 }
159                 else if(unode->maxgrid && dm->getGridData) {
160                         /* multires restore */
161                         DMGridData **grids, *grid;
162                         float (*co)[3];
163                         int gridsize;
164
165                         if(dm->getNumGrids(dm) != unode->maxgrid)
166                                 continue;
167                         if(dm->getGridSize(dm) != unode->gridsize)
168                                 continue;
169
170                         grids= dm->getGridData(dm);
171                         gridsize= dm->getGridSize(dm);
172
173                         co = unode->co;
174                         for(j=0; j<unode->totgrid; j++) {
175                                 grid= grids[unode->grids[j]];
176
177                                 for(i=0; i<gridsize*gridsize; i++, co++)
178                                         swap_v3_v3(grid[i].co, co[0]);
179                         }
180                 }
181
182                 update= 1;
183         }
184
185         if(update) {
186                 int tag_update= 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);
192
193                 if((mmd=sculpt_multires_active(scene, ob)))
194                         multires_mark_as_modified(ob);
195
196                 tag_update= ((Mesh*)ob->data)->id.us > 1;
197
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);
201
202                         sculpt_free_deformMats(ss);
203                         tag_update|= 1;
204                 }
205
206                 if(tag_update)
207                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
208
209                 /* for non-PBVH drawing, need to recreate VBOs */
210                 GPU_drawobject_free(ob->derivedFinal);
211         }
212 }
213
214 static void sculpt_undo_free(ListBase *lb)
215 {
216         SculptUndoNode *unode;
217
218         for(unode=lb->first; unode; unode=unode->next) {
219                 if(unode->co)
220                         MEM_freeN(unode->co);
221                 if(unode->no)
222                         MEM_freeN(unode->no);
223                 if(unode->index)
224                         MEM_freeN(unode->index);
225                 if(unode->grids)
226                         MEM_freeN(unode->grids);
227                 if(unode->layer_disp)
228                         MEM_freeN(unode->layer_disp);
229                 if(unode->orig_co)
230                         MEM_freeN(unode->orig_co);
231         }
232 }
233
234 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
235 {
236         ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
237         SculptUndoNode *unode;
238
239         if(!lb)
240                 return NULL;
241
242         for(unode=lb->first; unode; unode=unode->next)
243                 if(unode->node == node)
244                         return unode;
245
246         return NULL;
247 }
248
249 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node)
250 {
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;
255
256         /* list is manipulated by multiple threads, so we lock */
257         BLI_lock_thread(LOCK_CUSTOM1);
258
259         if((unode= sculpt_undo_get_node(node))) {
260                 BLI_unlock_thread(LOCK_CUSTOM1);
261                 return unode;
262         }
263
264         unode= MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
265         strcpy(unode->idname, ob->id.name);
266         unode->node= node;
267
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);
271
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);
278
279         if(maxgrid) {
280                 /* multires */
281                 unode->maxgrid= maxgrid;
282                 unode->totgrid= totgrid;
283                 unode->gridsize= gridsize;
284                 unode->grids= MEM_mapallocN(sizeof(int)*totgrid, "SculptUndoNode.grids");
285         }
286         else {
287                 /* regular mesh */
288                 unode->maxvert= ss->totvert;
289                 unode->index= MEM_mapallocN(sizeof(int)*allvert, "SculptUndoNode.index");
290         }
291
292         if(ss->modifiers_active)
293                 unode->orig_co= MEM_callocN(allvert*sizeof(*unode->orig_co), "undoSculpt orig_cos");
294
295         BLI_unlock_thread(LOCK_CUSTOM1);
296
297         /* copy threaded, hopefully this is the performance critical part */
298         {
299                 PBVHVertexIter vd;
300
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];
306
307                         if(ss->modifiers_active)
308                                 copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
309                 }
310                 BLI_pbvh_vertex_iter_end;
311         }
312
313         if(unode->grids)
314                 memcpy(unode->grids, grids, sizeof(int)*totgrid);
315
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';
319
320         return unode;
321 }
322
323 void sculpt_undo_push_begin(const char *name)
324 {
325         undo_paint_push_begin(UNDO_PAINT_MESH, name,
326                 sculpt_undo_restore, sculpt_undo_free);
327 }
328
329 void sculpt_undo_push_end(void)
330 {
331         ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
332         SculptUndoNode *unode;
333
334         /* we don't need normals in the undo stack */
335         for(unode=lb->first; unode; unode=unode->next) {
336                 if(unode->no) {
337                         MEM_freeN(unode->no);
338                         unode->no= NULL;
339                 }
340
341                 if(unode->layer_disp) {
342                         MEM_freeN(unode->layer_disp);
343                         unode->layer_disp= NULL;
344                 }
345         }
346
347         undo_paint_push_end(UNDO_PAINT_MESH);
348 }