svn merge ^/trunk/blender -r47961:48070
[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_ccg.h"
51 #include "BKE_cdderivedmesh.h"
52 #include "BKE_context.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_modifier.h"
55 #include "BKE_multires.h"
56 #include "BKE_paint.h"
57 #include "BKE_key.h"
58 #include "BKE_mesh.h"
59 #include "BKE_subsurf.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 *rebuild)
73 {
74         BLI_pbvh_node_mark_update(node);
75         if (*((int *)rebuild))
76                 BLI_pbvh_node_mark_rebuild_draw(node);
77         BLI_pbvh_node_fully_hidden_set(node, 0);
78 }
79
80 static void sculpt_undo_restore_deformed(const SculptSession *ss,
81                                          SculptUndoNode *unode,
82                                          int uindex, int oindex,
83                                          float coord[3])
84 {
85         if (unode->orig_co) {
86                 swap_v3_v3(coord, unode->orig_co[uindex]);
87                 copy_v3_v3(unode->co[uindex], ss->deform_cos[oindex]);
88         }
89         else {
90                 swap_v3_v3(coord, unode->co[uindex]);
91         }
92 }
93
94 static int sculpt_undo_restore_coords(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
95 {
96         Scene *scene = CTX_data_scene(C);
97         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
98         Object *ob = CTX_data_active_object(C);
99         SculptSession *ss = ob->sculpt;
100         MVert *mvert;
101         int *index, i, j;       
102         
103         if (unode->maxvert) {
104                 /* regular mesh restore */
105
106                 if (ss->kb && strcmp(ss->kb->name, unode->shapeName)) {
107                         /* shape key has been changed before calling undo operator */
108
109                         Key *key = ob_get_key(ob);
110                         KeyBlock *kb = key_get_named_keyblock(key, unode->shapeName);
111
112                         if (kb) {
113                                 ob->shapenr = BLI_findindex(&key->block, kb) + 1;
114
115                                 sculpt_update_mesh_elements(scene, sd, ob, 0);
116                                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, ob);
117                         }
118                         else {
119                                 /* key has been removed -- skip this undo node */
120                                 return 0;
121                         }
122                 }
123
124                 index = unode->index;
125                 mvert = ss->mvert;
126
127                 if (ss->kb) {
128                         float (*vertCos)[3];
129                         vertCos = key_to_vertcos(ob, ss->kb);
130
131                         for (i = 0; i < unode->totvert; i++) {
132                                 if (ss->modifiers_active) sculpt_undo_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
133                                 else {
134                                         if (unode->orig_co) swap_v3_v3(vertCos[index[i]], unode->orig_co[i]);
135                                         else swap_v3_v3(vertCos[index[i]], unode->co[i]);
136                                 }
137                         }
138
139                         /* propagate new coords to keyblock */
140                         sculpt_vertcos_to_key(ob, ss->kb, vertCos);
141
142                         /* pbvh uses it's own mvert array, so coords should be */
143                         /* propagated to pbvh here */
144                         BLI_pbvh_apply_vertCos(ss->pbvh, vertCos);
145
146                         MEM_freeN(vertCos);
147                 }
148                 else {
149                         for (i = 0; i < unode->totvert; i++) {
150                                 if (ss->modifiers_active) sculpt_undo_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                 CCGElem **grids, *grid;
162                 CCGKey key;
163                 float (*co)[3];
164                 int gridsize;
165
166                 grids = dm->getGridData(dm);
167                 gridsize = dm->getGridSize(dm);
168                 dm->getGridKey(dm, &key);
169
170                 co = unode->co;
171                 for (j = 0; j < unode->totgrid; j++) {
172                         grid = grids[unode->grids[j]];
173
174                         for (i = 0; i < gridsize * gridsize; i++, co++)
175                                 swap_v3_v3(CCG_elem_offset_co(&key, grid, i), co[0]);
176                 }
177         }
178
179         return 1;
180 }
181
182 static int sculpt_undo_restore_hidden(bContext *C, DerivedMesh *dm,
183                                       SculptUndoNode *unode)
184 {
185         Object *ob = CTX_data_active_object(C);
186         SculptSession *ss = ob->sculpt;
187         int i;
188
189         if (unode->maxvert) {
190                 MVert *mvert = ss->mvert;
191                 
192                 for (i = 0; i < unode->totvert; i++) {
193                         MVert *v = &mvert[unode->index[i]];
194                         int uval = BLI_BITMAP_GET(unode->vert_hidden, i);
195
196                         BLI_BITMAP_MODIFY(unode->vert_hidden, i,
197                                           v->flag & ME_HIDE);
198                         if (uval)
199                                 v->flag |= ME_HIDE;
200                         else
201                                 v->flag &= ~ME_HIDE;
202                         
203                         v->flag |= ME_VERT_PBVH_UPDATE;
204                 }
205         }
206         else if (unode->maxgrid && dm->getGridData) {
207                 BLI_bitmap *grid_hidden = dm->getGridHidden(dm);
208                 
209                 for (i = 0; i < unode->totgrid; i++) {
210                         SWAP(BLI_bitmap,
211                              unode->grid_hidden[i],
212                              grid_hidden[unode->grids[i]]);
213                         
214                 }
215         }
216
217         return 1;
218 }
219
220 static int sculpt_undo_restore_mask(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
221 {
222         Object *ob = CTX_data_active_object(C);
223         SculptSession *ss = ob->sculpt;
224         MVert *mvert;
225         float *vmask;
226         int *index, i, j;       
227         
228         if (unode->maxvert) {
229                 /* regular mesh restore */
230
231                 index = unode->index;
232                 mvert = ss->mvert;
233                 vmask = ss->vmask;
234
235                 for (i = 0; i < unode->totvert; i++) {
236                         SWAP(float, vmask[index[i]], unode->mask[i]);
237                         mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
238                 }
239         }
240         else if (unode->maxgrid && dm->getGridData) {
241                 /* multires restore */
242                 CCGElem **grids, *grid;
243                 CCGKey key;
244                 float *mask;
245                 int gridsize;
246
247                 grids = dm->getGridData(dm);
248                 gridsize = dm->getGridSize(dm);
249                 dm->getGridKey(dm, &key);
250
251                 mask = unode->mask;
252                 for (j = 0; j < unode->totgrid; j++) {
253                         grid = grids[unode->grids[j]];
254
255                         for (i = 0; i < gridsize * gridsize; i++, mask++)
256                                 SWAP(float, *CCG_elem_offset_mask(&key, grid, i), *mask);
257                 }
258         }
259
260         return 1;
261 }
262
263 static void sculpt_undo_restore(bContext *C, ListBase *lb)
264 {
265         Scene *scene = CTX_data_scene(C);
266         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
267         Object *ob = CTX_data_active_object(C);
268         DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0);
269         SculptSession *ss = ob->sculpt;
270         SculptUndoNode *unode;
271         MultiresModifierData *mmd;
272         int update = FALSE, rebuild = FALSE;
273
274         sculpt_update_mesh_elements(scene, sd, ob, 0);
275
276         for (unode = lb->first; unode; unode = unode->next) {
277                 if (!(strcmp(unode->idname, ob->id.name) == 0))
278                         continue;
279
280                 /* check if undo data matches current data well enough to
281                  * continue */
282                 if (unode->maxvert) {
283                         if (ss->totvert != unode->maxvert)
284                                 continue;
285                 }
286                 else if (unode->maxgrid && dm->getGridData) {
287                         if ((dm->getNumGrids(dm) != unode->maxgrid) ||
288                             (dm->getGridSize(dm) != unode->gridsize))
289                         {
290                                 continue;
291                         }
292                 }
293                 else {
294                         continue;
295                 }
296
297                 switch (unode->type) {
298                         case SCULPT_UNDO_COORDS:
299                                 if (sculpt_undo_restore_coords(C, dm, unode))
300                                         update = TRUE;
301                                 break;
302                         case SCULPT_UNDO_HIDDEN:
303                                 if (sculpt_undo_restore_hidden(C, dm, unode))
304                                         rebuild = TRUE;
305                                 break;
306                         case SCULPT_UNDO_MASK:
307                                 if (sculpt_undo_restore_mask(C, dm, unode))
308                                         update = TRUE;
309                                 break;
310                 }
311         }
312
313         if (update || rebuild) {
314                 int tag_update = 0;
315                 /* we update all nodes still, should be more clever, but also
316                  * needs to work correct when exiting/entering sculpt mode and
317                  * the nodes get recreated, though in that case it could do all */
318                 BLI_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
319                 BLI_pbvh_update(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw, NULL);
320
321                 if ((mmd = sculpt_multires_active(scene, ob))) {
322                         if (rebuild)
323                                 multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
324                         else
325                                 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
326                 }
327
328                 tag_update = ((Mesh *)ob->data)->id.us > 1;
329
330                 if (ss->modifiers_active) {
331                         Mesh *mesh = ob->data;
332                         BKE_mesh_calc_normals_tessface(mesh->mvert, mesh->totvert,
333                                                        mesh->mface, mesh->totface, NULL);
334
335                         free_sculptsession_deformMats(ss);
336                         tag_update |= 1;
337                 }
338
339                 if (tag_update)
340                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
341
342                 /* for non-PBVH drawing, need to recreate VBOs */
343                 GPU_drawobject_free(ob->derivedFinal);
344         }
345 }
346
347 static void sculpt_undo_free(ListBase *lb)
348 {
349         SculptUndoNode *unode;
350         int i;
351
352         for (unode = lb->first; unode; unode = unode->next) {
353                 if (unode->co)
354                         MEM_freeN(unode->co);
355                 if (unode->no)
356                         MEM_freeN(unode->no);
357                 if (unode->index)
358                         MEM_freeN(unode->index);
359                 if (unode->grids)
360                         MEM_freeN(unode->grids);
361                 if (unode->layer_disp)
362                         MEM_freeN(unode->layer_disp);
363                 if (unode->orig_co)
364                         MEM_freeN(unode->orig_co);
365                 if (unode->vert_hidden)
366                         MEM_freeN(unode->vert_hidden);
367                 if (unode->grid_hidden) {
368                         for (i = 0; i < unode->totgrid; i++) {
369                                 if (unode->grid_hidden[i])
370                                         MEM_freeN(unode->grid_hidden[i]);
371                         }
372                         MEM_freeN(unode->grid_hidden);
373                 }
374                 if (unode->mask)
375                         MEM_freeN(unode->mask);
376         }
377 }
378
379 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
380 {
381         ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
382         SculptUndoNode *unode;
383
384         if (!lb)
385                 return NULL;
386
387         for (unode = lb->first; unode; unode = unode->next)
388                 if (unode->node == node)
389                         return unode;
390
391         return NULL;
392 }
393
394 static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh,
395                                                SculptUndoNode *unode)
396 {
397         PBVHNode *node = unode->node;
398         BLI_bitmap *grid_hidden;
399         int i, *grid_indices, totgrid;
400
401         grid_hidden = BLI_pbvh_grid_hidden(pbvh);
402
403         BLI_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid,
404                                 NULL, NULL, NULL, NULL);
405                         
406         unode->grid_hidden = MEM_mapallocN(sizeof(BLI_bitmap) * totgrid,
407                                            "unode->grid_hidden");
408                 
409         for (i = 0; i < totgrid; i++) {
410                 if (grid_hidden[grid_indices[i]])
411                         unode->grid_hidden[i] = MEM_dupallocN(grid_hidden[grid_indices[i]]);
412                 else
413                         unode->grid_hidden[i] = NULL;
414         }
415 }
416
417 static SculptUndoNode *sculpt_undo_alloc_node(Object *ob, PBVHNode *node,
418                                               SculptUndoType type)
419 {
420         ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
421         SculptUndoNode *unode;
422         SculptSession *ss = ob->sculpt;
423         int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
424         
425         unode = MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
426         BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
427         unode->type = type;
428         unode->node = node;
429
430         BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
431         BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
432                                 &maxgrid, &gridsize, NULL, NULL);
433
434         unode->totvert = totvert;
435         
436         /* we will use this while sculpting, is mapalloc slow to access then? */
437
438         /* general TODO, fix count_alloc */
439         switch (type) {
440                 case SCULPT_UNDO_COORDS:
441                         unode->co = MEM_mapallocN(sizeof(float) * 3 * allvert, "SculptUndoNode.co");
442                         unode->no = MEM_mapallocN(sizeof(short) * 3 * allvert, "SculptUndoNode.no");
443                         undo_paint_push_count_alloc(UNDO_PAINT_MESH,
444                                                     (sizeof(float) * 3 +
445                                                      sizeof(short) * 3 +
446                                                      sizeof(int)) * allvert);
447                         break;
448                 case SCULPT_UNDO_HIDDEN:
449                         if (maxgrid)
450                                 sculpt_undo_alloc_and_store_hidden(ss->pbvh, unode);
451                         else
452                                 unode->vert_hidden = BLI_BITMAP_NEW(allvert, "SculptUndoNode.vert_hidden");
453                 
454                         break;
455                 case SCULPT_UNDO_MASK:
456                         unode->mask = MEM_mapallocN(sizeof(float) * allvert, "SculptUndoNode.mask");
457                         undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float) * sizeof(int)) * allvert);
458                         break;
459         }
460         
461         BLI_addtail(lb, unode);
462
463         if (maxgrid) {
464                 /* multires */
465                 unode->maxgrid = maxgrid;
466                 unode->totgrid = totgrid;
467                 unode->gridsize = gridsize;
468                 unode->grids = MEM_mapallocN(sizeof(int) * totgrid, "SculptUndoNode.grids");
469         }
470         else {
471                 /* regular mesh */
472                 unode->maxvert = ss->totvert;
473                 unode->index = MEM_mapallocN(sizeof(int) * allvert, "SculptUndoNode.index");
474         }
475
476         if (ss->modifiers_active)
477                 unode->orig_co = MEM_callocN(allvert * sizeof(*unode->orig_co), "undoSculpt orig_cos");
478
479         return unode;
480 }
481
482 static void sculpt_undo_store_coords(Object *ob, SculptUndoNode *unode)
483 {
484         SculptSession *ss = ob->sculpt;
485         PBVHVertexIter vd;
486
487         BLI_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
488         {
489                 copy_v3_v3(unode->co[vd.i], vd.co);
490                 if (vd.no) copy_v3_v3_short(unode->no[vd.i], vd.no);
491                 else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
492
493                 if (ss->modifiers_active)
494                         copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
495         }
496         BLI_pbvh_vertex_iter_end;
497 }
498
499 static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
500 {
501         PBVH *pbvh = ob->sculpt->pbvh;
502         PBVHNode *node = unode->node;
503
504         if (unode->grids) {
505                 /* already stored during allocation */
506         }
507         else {
508                 MVert *mvert;
509                 int *vert_indices, allvert;
510                 int i;
511                 
512                 BLI_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
513                 BLI_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
514                 for (i = 0; i < allvert; i++) {
515                         BLI_BITMAP_MODIFY(unode->vert_hidden, i,
516                                           mvert[vert_indices[i]].flag & ME_HIDE);
517                 }
518         }
519 }
520
521 static void sculpt_undo_store_mask(Object *ob, SculptUndoNode *unode)
522 {
523         SculptSession *ss = ob->sculpt;
524         PBVHVertexIter vd;
525
526         BLI_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
527         {
528                 unode->mask[vd.i] = *vd.mask;
529         }
530         BLI_pbvh_vertex_iter_end;
531 }
532
533 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node,
534                                       SculptUndoType type)
535 {
536         SculptSession *ss = ob->sculpt;
537         SculptUndoNode *unode;
538
539         /* list is manipulated by multiple threads, so we lock */
540         BLI_lock_thread(LOCK_CUSTOM1);
541
542         if ((unode = sculpt_undo_get_node(node))) {
543                 BLI_unlock_thread(LOCK_CUSTOM1);
544                 return unode;
545         }
546
547         unode = sculpt_undo_alloc_node(ob, node, type);
548         
549         BLI_unlock_thread(LOCK_CUSTOM1);
550
551         /* copy threaded, hopefully this is the performance critical part */
552
553         if (unode->grids) {
554                 int totgrid, *grids;
555                 BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
556                                         NULL, NULL, NULL, NULL);
557                 memcpy(unode->grids, grids, sizeof(int) * totgrid);
558         }
559         else {
560                 int *vert_indices, allvert;
561                 BLI_pbvh_node_num_verts(ss->pbvh, node, NULL, &allvert);
562                 BLI_pbvh_node_get_verts(ss->pbvh, node, &vert_indices, NULL);
563                 memcpy(unode->index, vert_indices, sizeof(int) * unode->totvert);
564         }
565
566         switch (type) {
567                 case SCULPT_UNDO_COORDS:
568                         sculpt_undo_store_coords(ob, unode);
569                         break;
570                 case SCULPT_UNDO_HIDDEN:
571                         sculpt_undo_store_hidden(ob, unode);
572                         break;
573                 case SCULPT_UNDO_MASK:
574                         sculpt_undo_store_mask(ob, unode);
575                         break;
576         }
577
578         /* store active shape key */
579         if (ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
580         else unode->shapeName[0] = '\0';
581
582         return unode;
583 }
584
585 void sculpt_undo_push_begin(const char *name)
586 {
587         undo_paint_push_begin(UNDO_PAINT_MESH, name,
588                               sculpt_undo_restore, sculpt_undo_free);
589 }
590
591 void sculpt_undo_push_end(void)
592 {
593         ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
594         SculptUndoNode *unode;
595
596         /* we don't need normals in the undo stack */
597         for (unode = lb->first; unode; unode = unode->next) {
598                 if (unode->no) {
599                         MEM_freeN(unode->no);
600                         unode->no = NULL;
601                 }
602
603                 if (unode->layer_disp) {
604                         MEM_freeN(unode->layer_disp);
605                         unode->layer_disp = NULL;
606                 }
607         }
608
609         undo_paint_push_end(UNDO_PAINT_MESH);
610 }