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