Merging r45936 through r46042 from trunk into soc-2011-tomato
[blender-staging.git] / source / blender / editors / sculpt_paint / sculpt_undo.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2006 by Nicholas Bishop
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  * Implements the Sculpt Mode tools
28  *
29  */
30
31 /** \file blender/editors/sculpt_paint/sculpt_undo.c
32  *  \ingroup edsculpt
33  */
34
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_math.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"
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 #include "BKE_subsurf.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "GPU_buffers.h"
64
65 #include "ED_sculpt.h"
66 #include "paint_intern.h"
67 #include "sculpt_intern.h"
68
69 /************************** Undo *************************/
70
71 static void update_cb(PBVHNode *node, void *rebuild)
72 {
73         BLI_pbvh_node_mark_update(node);
74         if (*((int *)rebuild))
75                 BLI_pbvh_node_mark_rebuild_draw(node);
76         BLI_pbvh_node_fully_hidden_set(node, 0);
77 }
78
79 static void sculpt_undo_restore_deformed(SculptSession *ss, SculptUndoNode *unode, int uindex, int oindex, float coord[3])
80 {
81         if (unode->orig_co) {
82                 swap_v3_v3(coord, unode->orig_co[uindex]);
83                 copy_v3_v3(unode->co[uindex], ss->deform_cos[oindex]);
84         }
85         else {
86                 swap_v3_v3(coord, unode->co[uindex]);
87         }
88 }
89
90 static int sculpt_undo_restore_coords(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
91 {
92         Scene *scene = CTX_data_scene(C);
93         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
94         Object *ob = CTX_data_active_object(C);
95         SculptSession *ss = ob->sculpt;
96         MVert *mvert;
97         int *index, i, j;       
98         
99         if (unode->maxvert) {
100                 /* regular mesh restore */
101
102                 if (ss->kb && strcmp(ss->kb->name, unode->shapeName)) {
103                         /* shape key has been changed before calling undo operator */
104
105                         Key *key = ob_get_key(ob);
106                         KeyBlock *kb = key_get_named_keyblock(key, unode->shapeName);
107
108                         if (kb) {
109                                 ob->shapenr = BLI_findindex(&key->block, kb) + 1;
110
111                                 sculpt_update_mesh_elements(scene, sd, ob, 0);
112                                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, ob);
113                         }
114                         else {
115                                 /* key has been removed -- skip this undo node */
116                                 return 0;
117                         }
118                 }
119
120                 index = unode->index;
121                 mvert = ss->mvert;
122
123                 if (ss->kb) {
124                         float (*vertCos)[3];
125                         vertCos = key_to_vertcos(ob, ss->kb);
126
127                         for (i = 0; i < unode->totvert; i++) {
128                                 if (ss->modifiers_active) sculpt_undo_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
129                                 else {
130                                         if (unode->orig_co) swap_v3_v3(vertCos[index[i]], unode->orig_co[i]);
131                                         else swap_v3_v3(vertCos[index[i]], unode->co[i]);
132                                 }
133                         }
134
135                         /* propagate new coords to keyblock */
136                         sculpt_vertcos_to_key(ob, ss->kb, vertCos);
137
138                         /* pbvh uses it's own mvert array, so coords should be */
139                         /* propagated to pbvh here */
140                         BLI_pbvh_apply_vertCos(ss->pbvh, vertCos);
141
142                         MEM_freeN(vertCos);
143                 }
144                 else {
145                         for (i = 0; i < unode->totvert; i++) {
146                                 if (ss->modifiers_active) sculpt_undo_restore_deformed(ss, unode, i, index[i], mvert[index[i]].co);
147                                 else {
148                                         if (unode->orig_co) swap_v3_v3(mvert[index[i]].co, unode->orig_co[i]);
149                                         else swap_v3_v3(mvert[index[i]].co, unode->co[i]);
150                                 }
151                                 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
152                         }
153                 }
154         }
155         else if (unode->maxgrid && dm->getGridData) {
156                 /* multires restore */
157                 DMGridData **grids, *grid;
158                 float (*co)[3];
159                 int gridsize;
160
161                 grids = dm->getGridData(dm);
162                 gridsize = dm->getGridSize(dm);
163
164                 co = unode->co;
165                 for (j = 0; j < unode->totgrid; j++) {
166                         grid = grids[unode->grids[j]];
167
168                         for (i = 0; i < gridsize * gridsize; i++, co++)
169                                 swap_v3_v3(grid[i].co, co[0]);
170                 }
171         }
172
173         return 1;
174 }
175
176 static int sculpt_undo_restore_hidden(bContext *C, DerivedMesh *dm,
177                                       SculptUndoNode *unode)
178 {
179         Object *ob = CTX_data_active_object(C);
180         SculptSession *ss = ob->sculpt;
181         int i;
182
183         if (unode->maxvert) {
184                 MVert *mvert = ss->mvert;
185                 
186                 for (i = 0; i < unode->totvert; i++) {
187                         MVert *v = &mvert[unode->index[i]];
188                         int uval = BLI_BITMAP_GET(unode->vert_hidden, i);
189
190                         BLI_BITMAP_MODIFY(unode->vert_hidden, i,
191                                           v->flag & ME_HIDE);
192                         if (uval)
193                                 v->flag |= ME_HIDE;
194                         else
195                                 v->flag &= ~ME_HIDE;
196                         
197                         v->flag |= ME_VERT_PBVH_UPDATE;
198                 }
199         }
200         else if (unode->maxgrid && dm->getGridData) {
201                 BLI_bitmap *grid_hidden = dm->getGridHidden(dm);
202                 
203                 for (i = 0; i < unode->totgrid; i++) {
204                         SWAP(BLI_bitmap,
205                              unode->grid_hidden[i],
206                              grid_hidden[unode->grids[i]]);
207                         
208                 }
209         }
210
211         return 1;
212 }
213
214 static void sculpt_undo_restore(bContext *C, ListBase *lb)
215 {
216         Scene *scene = CTX_data_scene(C);
217         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
218         Object *ob = CTX_data_active_object(C);
219         DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0);
220         SculptSession *ss = ob->sculpt;
221         SculptUndoNode *unode;
222         MultiresModifierData *mmd;
223         int update = 0, rebuild = 1;
224
225         sculpt_update_mesh_elements(scene, sd, ob, 0);
226
227         for (unode = lb->first; unode; unode = unode->next) {
228                 if (!(strcmp(unode->idname, ob->id.name) == 0))
229                         continue;
230
231                 /* check if undo data matches current data well enough to
232                  * continue */
233                 if (unode->maxvert) {
234                         if (ss->totvert != unode->maxvert)
235                                 continue;
236                 }
237                 else if (unode->maxgrid && dm->getGridData) {
238                         if ((dm->getNumGrids(dm) != unode->maxgrid) ||
239                             (dm->getGridSize(dm) != unode->gridsize))
240                         {
241                                 continue;
242                         }
243                 }
244                 else {
245                         continue;
246                 }
247
248                 switch (unode->type) {
249                         case SCULPT_UNDO_COORDS:
250                                 if (sculpt_undo_restore_coords(C, dm, unode))
251                                         update = 1;
252                                 break;
253                         case SCULPT_UNDO_HIDDEN:
254                                 if (sculpt_undo_restore_hidden(C, dm, unode))
255                                         rebuild = 1;
256                                 break;
257                 }
258         }
259
260         if (update || rebuild) {
261                 int tag_update = 0;
262                 /* we update all nodes still, should be more clever, but also
263                  * needs to work correct when exiting/entering sculpt mode and
264                  * the nodes get recreated, though in that case it could do all */
265                 BLI_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
266                 BLI_pbvh_update(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw, NULL);
267
268                 if ((mmd = sculpt_multires_active(scene, ob))) {
269                         if (rebuild)
270                                 multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
271                         else
272                                 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
273                 }
274
275                 tag_update = ((Mesh *)ob->data)->id.us > 1;
276
277                 if (ss->modifiers_active) {
278                         Mesh *mesh = ob->data;
279                         mesh_calc_normals_tessface(mesh->mvert, mesh->totvert,
280                                                                            mesh->mface, mesh->totface, NULL);
281
282                         free_sculptsession_deformMats(ss);
283                         tag_update |= 1;
284                 }
285
286                 if (tag_update)
287                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
288
289                 /* for non-PBVH drawing, need to recreate VBOs */
290                 GPU_drawobject_free(ob->derivedFinal);
291         }
292 }
293
294 static void sculpt_undo_free(ListBase *lb)
295 {
296         SculptUndoNode *unode;
297         int i;
298
299         for (unode = lb->first; unode; unode = unode->next) {
300                 if (unode->co)
301                         MEM_freeN(unode->co);
302                 if (unode->no)
303                         MEM_freeN(unode->no);
304                 if (unode->index)
305                         MEM_freeN(unode->index);
306                 if (unode->grids)
307                         MEM_freeN(unode->grids);
308                 if (unode->layer_disp)
309                         MEM_freeN(unode->layer_disp);
310                 if (unode->orig_co)
311                         MEM_freeN(unode->orig_co);
312                 if (unode->vert_hidden)
313                         MEM_freeN(unode->vert_hidden);
314                 if (unode->grid_hidden) {
315                         for (i = 0; i < unode->totgrid; i++) {
316                                 if (unode->grid_hidden[i])
317                                         MEM_freeN(unode->grid_hidden[i]);
318                         }
319                         MEM_freeN(unode->grid_hidden);
320                 }
321         }
322 }
323
324 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
325 {
326         ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
327         SculptUndoNode *unode;
328
329         if (!lb)
330                 return NULL;
331
332         for (unode = lb->first; unode; unode = unode->next)
333                 if (unode->node == node)
334                         return unode;
335
336         return NULL;
337 }
338
339 static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh,
340                                                SculptUndoNode *unode)
341 {
342         PBVHNode *node = unode->node;
343         BLI_bitmap *grid_hidden;
344         int i, *grid_indices, totgrid;
345
346         grid_hidden = BLI_pbvh_grid_hidden(pbvh);
347
348         BLI_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid,
349                                 NULL, NULL, NULL, NULL);
350                         
351         unode->grid_hidden = MEM_mapallocN(sizeof(BLI_bitmap) * totgrid,
352                                            "unode->grid_hidden");
353                 
354         for (i = 0; i < totgrid; i++) {
355                 if (grid_hidden[grid_indices[i]])
356                         unode->grid_hidden[i] = MEM_dupallocN(grid_hidden[grid_indices[i]]);
357                 else
358                         unode->grid_hidden[i] = NULL;
359         }
360 }
361
362 static SculptUndoNode *sculpt_undo_alloc_node(Object *ob, PBVHNode *node,
363                                               SculptUndoType type)
364 {
365         ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
366         SculptUndoNode *unode;
367         SculptSession *ss = ob->sculpt;
368         int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
369         
370         unode = MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
371         BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
372         unode->type = type;
373         unode->node = node;
374
375         BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
376         BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
377                                 &maxgrid, &gridsize, NULL, NULL);
378
379         unode->totvert = totvert;
380         
381         /* we will use this while sculpting, is mapalloc slow to access then? */
382
383         /* general TODO, fix count_alloc */
384         switch (type) {
385                 case SCULPT_UNDO_COORDS:
386                         unode->co = MEM_mapallocN(sizeof(float) * 3 * allvert, "SculptUndoNode.co");
387                         unode->no = MEM_mapallocN(sizeof(short) * 3 * allvert, "SculptUndoNode.no");
388                         undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float) * 3 + sizeof(short) * 3 + sizeof(int)) * allvert);
389                         break;
390                 case SCULPT_UNDO_HIDDEN:
391                         if (maxgrid)
392                                 sculpt_undo_alloc_and_store_hidden(ss->pbvh, unode);
393                         else
394                                 unode->vert_hidden = BLI_BITMAP_NEW(allvert, "SculptUndoNode.vert_hidden");
395                 
396                         break;
397         }
398         
399         BLI_addtail(lb, unode);
400
401         if (maxgrid) {
402                 /* multires */
403                 unode->maxgrid = maxgrid;
404                 unode->totgrid = totgrid;
405                 unode->gridsize = gridsize;
406                 unode->grids = MEM_mapallocN(sizeof(int) * totgrid, "SculptUndoNode.grids");
407         }
408         else {
409                 /* regular mesh */
410                 unode->maxvert = ss->totvert;
411                 unode->index = MEM_mapallocN(sizeof(int) * allvert, "SculptUndoNode.index");
412         }
413
414         if (ss->modifiers_active)
415                 unode->orig_co = MEM_callocN(allvert * sizeof(*unode->orig_co), "undoSculpt orig_cos");
416
417         return unode;
418 }
419
420 static void sculpt_undo_store_coords(Object *ob, SculptUndoNode *unode)
421 {
422         SculptSession *ss = ob->sculpt;
423         PBVHVertexIter vd;
424
425         BLI_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL) {
426                 copy_v3_v3(unode->co[vd.i], vd.co);
427                 if (vd.no) copy_v3_v3_short(unode->no[vd.i], vd.no);
428                 else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
429
430                 if (ss->modifiers_active)
431                         copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
432         }
433         BLI_pbvh_vertex_iter_end;
434 }
435
436 static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
437 {
438         PBVH *pbvh = ob->sculpt->pbvh;
439         PBVHNode *node = unode->node;
440
441         if (unode->grids) {
442                 /* already stored during allocation */
443         }
444         else {
445                 MVert *mvert;
446                 int *vert_indices, allvert;
447                 int i;
448                 
449                 BLI_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
450                 BLI_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
451                 for (i = 0; i < allvert; i++) {
452                         BLI_BITMAP_MODIFY(unode->vert_hidden, i,
453                                           mvert[vert_indices[i]].flag & ME_HIDE);
454                 }
455         }
456 }
457
458 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node,
459                                       SculptUndoType type)
460 {
461         SculptSession *ss = ob->sculpt;
462         SculptUndoNode *unode;
463
464         /* list is manipulated by multiple threads, so we lock */
465         BLI_lock_thread(LOCK_CUSTOM1);
466
467         if ((unode = sculpt_undo_get_node(node))) {
468                 BLI_unlock_thread(LOCK_CUSTOM1);
469                 return unode;
470         }
471
472         unode = sculpt_undo_alloc_node(ob, node, type);
473         
474         BLI_unlock_thread(LOCK_CUSTOM1);
475
476         /* copy threaded, hopefully this is the performance critical part */
477
478         if (unode->grids) {
479                 int totgrid, *grids;
480                 BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
481                                         NULL, NULL, NULL, NULL);
482                 memcpy(unode->grids, grids, sizeof(int) * totgrid);
483         }
484         else {
485                 int *vert_indices, allvert;
486                 BLI_pbvh_node_num_verts(ss->pbvh, node, NULL, &allvert);
487                 BLI_pbvh_node_get_verts(ss->pbvh, node, &vert_indices, NULL);
488                 memcpy(unode->index, vert_indices, sizeof(int) * unode->totvert);
489         }
490
491         switch (type) {
492                 case SCULPT_UNDO_COORDS:
493                         sculpt_undo_store_coords(ob, unode);
494                         break;
495                 case SCULPT_UNDO_HIDDEN:
496                         sculpt_undo_store_hidden(ob, unode);
497                         break;
498         }
499
500         /* store active shape key */
501         if (ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
502         else unode->shapeName[0] = '\0';
503
504         return unode;
505 }
506
507 void sculpt_undo_push_begin(const char *name)
508 {
509         undo_paint_push_begin(UNDO_PAINT_MESH, name,
510                               sculpt_undo_restore, sculpt_undo_free);
511 }
512
513 void sculpt_undo_push_end(void)
514 {
515         ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
516         SculptUndoNode *unode;
517
518         /* we don't need normals in the undo stack */
519         for (unode = lb->first; unode; unode = unode->next) {
520                 if (unode->no) {
521                         MEM_freeN(unode->no);
522                         unode->no = NULL;
523                 }
524
525                 if (unode->layer_disp) {
526                         MEM_freeN(unode->layer_disp);
527                         unode->layer_disp = NULL;
528                 }
529         }
530
531         undo_paint_push_end(UNDO_PAINT_MESH);
532 }