Merging r44227 through r45619 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                                 continue;
241                 }
242                 else {
243                         continue;
244                 }
245
246                 switch (unode->type) {
247                         case SCULPT_UNDO_COORDS:
248                                 if (sculpt_undo_restore_coords(C, dm, unode))
249                                         update = 1;
250                                 break;
251                         case SCULPT_UNDO_HIDDEN:
252                                 if (sculpt_undo_restore_hidden(C, dm, unode))
253                                         rebuild = 1;
254                                 break;
255                 }
256         }
257
258         if (update || rebuild) {
259                 int tag_update = 0;
260                 /* we update all nodes still, should be more clever, but also
261                  * needs to work correct when exiting/entering sculpt mode and
262                  * the nodes get recreated, though in that case it could do all */
263                 BLI_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
264                 BLI_pbvh_update(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw, NULL);
265
266                 if ((mmd = sculpt_multires_active(scene, ob))) {
267                         if (rebuild)
268                                 multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
269                         else
270                                 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
271                 }
272
273                 tag_update = ((Mesh *)ob->data)->id.us > 1;
274
275                 if (ss->modifiers_active) {
276                         Mesh *mesh = ob->data;
277                         mesh_calc_normals_tessface(mesh->mvert, mesh->totvert,
278                                                                            mesh->mface, mesh->totface, NULL);
279
280                         free_sculptsession_deformMats(ss);
281                         tag_update |= 1;
282                 }
283
284                 if (tag_update)
285                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
286
287                 /* for non-PBVH drawing, need to recreate VBOs */
288                 GPU_drawobject_free(ob->derivedFinal);
289         }
290 }
291
292 static void sculpt_undo_free(ListBase *lb)
293 {
294         SculptUndoNode *unode;
295         int i;
296
297         for (unode = lb->first; unode; unode = unode->next) {
298                 if (unode->co)
299                         MEM_freeN(unode->co);
300                 if (unode->no)
301                         MEM_freeN(unode->no);
302                 if (unode->index)
303                         MEM_freeN(unode->index);
304                 if (unode->grids)
305                         MEM_freeN(unode->grids);
306                 if (unode->layer_disp)
307                         MEM_freeN(unode->layer_disp);
308                 if (unode->orig_co)
309                         MEM_freeN(unode->orig_co);
310                 if (unode->vert_hidden)
311                         MEM_freeN(unode->vert_hidden);
312                 if (unode->grid_hidden) {
313                         for (i = 0; i < unode->totgrid; i++) {
314                                 if (unode->grid_hidden[i])
315                                         MEM_freeN(unode->grid_hidden[i]);
316                         }
317                         MEM_freeN(unode->grid_hidden);
318                 }
319         }
320 }
321
322 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
323 {
324         ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
325         SculptUndoNode *unode;
326
327         if (!lb)
328                 return NULL;
329
330         for (unode = lb->first; unode; unode = unode->next)
331                 if (unode->node == node)
332                         return unode;
333
334         return NULL;
335 }
336
337 static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh,
338                                                SculptUndoNode *unode)
339 {
340         PBVHNode *node = unode->node;
341         BLI_bitmap *grid_hidden;
342         int i, *grid_indices, totgrid;
343
344         grid_hidden = BLI_pbvh_grid_hidden(pbvh);
345
346         BLI_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid,
347                                 NULL, NULL, NULL, NULL);
348                         
349         unode->grid_hidden = MEM_mapallocN(sizeof(BLI_bitmap) * totgrid,
350                                            "unode->grid_hidden");
351                 
352         for (i = 0; i < totgrid; i++) {
353                 if (grid_hidden[grid_indices[i]])
354                         unode->grid_hidden[i] = MEM_dupallocN(grid_hidden[grid_indices[i]]);
355                 else
356                         unode->grid_hidden[i] = NULL;
357         }
358 }
359
360 static SculptUndoNode *sculpt_undo_alloc_node(Object *ob, PBVHNode *node,
361                                               SculptUndoType type)
362 {
363         ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
364         SculptUndoNode *unode;
365         SculptSession *ss = ob->sculpt;
366         int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
367         
368         unode = MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
369         BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
370         unode->type = type;
371         unode->node = node;
372
373         BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
374         BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
375                                 &maxgrid, &gridsize, NULL, NULL);
376
377         unode->totvert = totvert;
378         
379         /* we will use this while sculpting, is mapalloc slow to access then? */
380
381         /* general TODO, fix count_alloc */
382         switch (type) {
383                 case SCULPT_UNDO_COORDS:
384                         unode->co = MEM_mapallocN(sizeof(float) * 3 * allvert, "SculptUndoNode.co");
385                         unode->no = MEM_mapallocN(sizeof(short) * 3 * allvert, "SculptUndoNode.no");
386                         undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float) * 3 + sizeof(short) * 3 + sizeof(int)) * allvert);
387                         break;
388                 case SCULPT_UNDO_HIDDEN:
389                         if (maxgrid)
390                                 sculpt_undo_alloc_and_store_hidden(ss->pbvh, unode);
391                         else
392                                 unode->vert_hidden = BLI_BITMAP_NEW(allvert, "SculptUndoNode.vert_hidden");
393                 
394                         break;
395         }
396         
397         BLI_addtail(lb, unode);
398
399         if (maxgrid) {
400                 /* multires */
401                 unode->maxgrid = maxgrid;
402                 unode->totgrid = totgrid;
403                 unode->gridsize = gridsize;
404                 unode->grids = MEM_mapallocN(sizeof(int) * totgrid, "SculptUndoNode.grids");
405         }
406         else {
407                 /* regular mesh */
408                 unode->maxvert = ss->totvert;
409                 unode->index = MEM_mapallocN(sizeof(int) * allvert, "SculptUndoNode.index");
410         }
411
412         if (ss->modifiers_active)
413                 unode->orig_co = MEM_callocN(allvert * sizeof(*unode->orig_co), "undoSculpt orig_cos");
414
415         return unode;
416 }
417
418 static void sculpt_undo_store_coords(Object *ob, SculptUndoNode *unode)
419 {
420         SculptSession *ss = ob->sculpt;
421         PBVHVertexIter vd;
422
423         BLI_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL) {
424                 copy_v3_v3(unode->co[vd.i], vd.co);
425                 if (vd.no) copy_v3_v3_short(unode->no[vd.i], vd.no);
426                 else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
427
428                 if (ss->modifiers_active)
429                         copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
430         }
431         BLI_pbvh_vertex_iter_end;
432 }
433
434 static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
435 {
436         PBVH *pbvh = ob->sculpt->pbvh;
437         PBVHNode *node = unode->node;
438
439         if (unode->grids) {
440                 /* already stored during allocation */
441         }
442         else {
443                 MVert *mvert;
444                 int *vert_indices, allvert;
445                 int i;
446                 
447                 BLI_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
448                 BLI_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
449                 for (i = 0; i < allvert; i++) {
450                         BLI_BITMAP_MODIFY(unode->vert_hidden, i,
451                                           mvert[vert_indices[i]].flag & ME_HIDE);
452                 }
453         }
454 }
455
456 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node,
457                                       SculptUndoType type)
458 {
459         SculptSession *ss = ob->sculpt;
460         SculptUndoNode *unode;
461
462         /* list is manipulated by multiple threads, so we lock */
463         BLI_lock_thread(LOCK_CUSTOM1);
464
465         if ((unode = sculpt_undo_get_node(node))) {
466                 BLI_unlock_thread(LOCK_CUSTOM1);
467                 return unode;
468         }
469
470         unode = sculpt_undo_alloc_node(ob, node, type);
471         
472         BLI_unlock_thread(LOCK_CUSTOM1);
473
474         /* copy threaded, hopefully this is the performance critical part */
475
476         if (unode->grids) {
477                 int totgrid, *grids;
478                 BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
479                                         NULL, NULL, NULL, NULL);
480                 memcpy(unode->grids, grids, sizeof(int) * totgrid);
481         }
482         else {
483                 int *vert_indices, allvert;
484                 BLI_pbvh_node_num_verts(ss->pbvh, node, NULL, &allvert);
485                 BLI_pbvh_node_get_verts(ss->pbvh, node, &vert_indices, NULL);
486                 memcpy(unode->index, vert_indices, sizeof(int) * unode->totvert);
487         }
488
489         switch (type) {
490                 case SCULPT_UNDO_COORDS:
491                         sculpt_undo_store_coords(ob, unode);
492                         break;
493                 case SCULPT_UNDO_HIDDEN:
494                         sculpt_undo_store_hidden(ob, unode);
495                         break;
496         }
497
498         /* store active shape key */
499         if (ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
500         else unode->shapeName[0] = '\0';
501
502         return unode;
503 }
504
505 void sculpt_undo_push_begin(const char *name)
506 {
507         undo_paint_push_begin(UNDO_PAINT_MESH, name,
508                               sculpt_undo_restore, sculpt_undo_free);
509 }
510
511 void sculpt_undo_push_end(void)
512 {
513         ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
514         SculptUndoNode *unode;
515
516         /* we don't need normals in the undo stack */
517         for (unode = lb->first; unode; unode = unode->next) {
518                 if (unode->no) {
519                         MEM_freeN(unode->no);
520                         unode->no = NULL;
521                 }
522
523                 if (unode->layer_disp) {
524                         MEM_freeN(unode->layer_disp);
525                         unode->layer_disp = NULL;
526                 }
527         }
528
529         undo_paint_push_end(UNDO_PAINT_MESH);
530 }