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