GSOC 2013 paint
[blender-staging.git] / source / blender / editors / sculpt_paint / sculpt_undo.c
index 2cc09ea2aa98468b993237d0c850e6fc60954283..614c1f3ef1d4c7b21938ef7725a52e073763d592 100644 (file)
 #include "DNA_mesh_types.h"
 
 #include "BKE_ccg.h"
-#include "BKE_cdderivedmesh.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
-#include "BKE_modifier.h"
 #include "BKE_multires.h"
 #include "BKE_paint.h"
 #include "BKE_key.h"
@@ -64,7 +62,8 @@
 
 #include "GPU_buffers.h"
 
-#include "ED_sculpt.h"
+#include "ED_paint.h"
+
 #include "bmesh.h"
 #include "paint_intern.h"
 #include "sculpt_intern.h"
@@ -74,7 +73,7 @@
 static void update_cb(PBVHNode *node, void *rebuild)
 {
        BKE_pbvh_node_mark_update(node);
-       if (*((int *)rebuild))
+       if (*((bool *)rebuild))
                BKE_pbvh_node_mark_rebuild_draw(node);
        BKE_pbvh_node_fully_hidden_set(node, 0);
 }
@@ -114,7 +113,7 @@ static int sculpt_undo_restore_coords(bContext *C, DerivedMesh *dm, SculptUndoNo
                        if (kb) {
                                ob->shapenr = BLI_findindex(&key->block, kb) + 1;
 
-                               sculpt_update_mesh_elements(scene, sd, ob, 0, FALSE);
+                               BKE_sculpt_update_mesh_elements(scene, sd, ob, 0, false);
                                WM_event_add_notifier(C, NC_OBJECT | ND_DATA, ob);
                        }
                        else {
@@ -197,9 +196,9 @@ static int sculpt_undo_restore_hidden(bContext *C, DerivedMesh *dm,
                
                for (i = 0; i < unode->totvert; i++) {
                        MVert *v = &mvert[unode->index[i]];
-                       int uval = BLI_BITMAP_GET(unode->vert_hidden, i);
+                       int uval = BLI_BITMAP_TEST(unode->vert_hidden, i);
 
-                       BLI_BITMAP_MODIFY(unode->vert_hidden, i,
+                       BLI_BITMAP_SET(unode->vert_hidden, i,
                                          v->flag & ME_HIDE);
                        if (uval)
                                v->flag |= ME_HIDE;
@@ -210,10 +209,10 @@ static int sculpt_undo_restore_hidden(bContext *C, DerivedMesh *dm,
                }
        }
        else if (unode->maxgrid && dm->getGridData) {
-               BLI_bitmap *grid_hidden = dm->getGridHidden(dm);
+               BLI_bitmap **grid_hidden = dm->getGridHidden(dm);
                
                for (i = 0; i < unode->totgrid; i++) {
-                       SWAP(BLI_bitmap,
+                       SWAP(BLI_bitmap *,
                             unode->grid_hidden[i],
                             grid_hidden[unode->grids[i]]);
                        
@@ -266,22 +265,43 @@ static int sculpt_undo_restore_mask(bContext *C, DerivedMesh *dm, SculptUndoNode
        return 1;
 }
 
-static void sculpt_undo_bmesh_restore_generic(SculptUndoNode *unode,
+static void sculpt_undo_bmesh_restore_generic(bContext *C,
+                                              SculptUndoNode *unode,
                                               Object *ob,
                                               SculptSession *ss)
 {
        if (unode->applied) {
                BM_log_undo(ss->bm, ss->bm_log);
-               unode->applied = FALSE;
+               unode->applied = false;
        }
        else {
                BM_log_redo(ss->bm, ss->bm_log);
-               unode->applied = TRUE;
+               unode->applied = true;
        }
 
-       /* A bit lame, but for now just recreate the PBVH. The alternative
-        * is to store changes to the PBVH in the undo stack. */
-       sculpt_pbvh_clear(ob);
+       if (ELEM(unode->type, SCULPT_UNDO_MASK, SCULPT_UNDO_MASK)) {
+               int i, totnode;
+               PBVHNode **nodes;
+
+#ifdef _OPENMP
+               Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
+#else
+               (void)C;
+#endif
+
+               BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
+
+#pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
+               for (i = 0; i < totnode; i++) {
+                       BKE_pbvh_node_mark_redraw(nodes[i]);
+               }
+
+               if (nodes)
+                       MEM_freeN(nodes);
+       }
+       else {
+               sculpt_pbvh_clear(ob);
+       }
 }
 
 /* Create empty sculpt BMesh and enable logging */
@@ -296,6 +316,7 @@ static void sculpt_undo_bmesh_enable(Object *ob,
        /* Create empty BMesh and enable logging */
        ss->bm = BM_mesh_create(&bm_mesh_allocsize_default);
        BM_data_layer_add(ss->bm, &ss->bm->vdata, CD_PAINT_MASK);
+       sculpt_dyntopo_node_layers_add(ss);
        me->flag |= ME_SCULPT_DYNAMIC_TOPOLOGY;
 
        /* Restore the BMLog using saved entries */
@@ -310,7 +331,7 @@ static void sculpt_undo_bmesh_restore_begin(bContext *C,
 {
        if (unode->applied) {
                sculpt_dynamic_topology_disable(C, unode);
-               unode->applied = FALSE;
+               unode->applied = false;
        }
        else {
                sculpt_undo_bmesh_enable(ob, unode);
@@ -318,7 +339,7 @@ static void sculpt_undo_bmesh_restore_begin(bContext *C,
                /* Restore the mesh from the first log entry */
                BM_log_redo(ss->bm, ss->bm_log);
 
-               unode->applied = TRUE;
+               unode->applied = true;
        }
 }
 
@@ -333,19 +354,19 @@ static void sculpt_undo_bmesh_restore_end(bContext *C,
                /* Restore the mesh from the last log entry */
                BM_log_undo(ss->bm, ss->bm_log);
 
-               unode->applied = FALSE;
+               unode->applied = false;
        }
        else {
                /* Disable dynamic topology sculpting */
                sculpt_dynamic_topology_disable(C, NULL);
-               unode->applied = TRUE;
+               unode->applied = true;
        }
 }
 
 /* Handle all dynamic-topology updates
  *
- * Returns TRUE if this was a dynamic-topology undo step, otherwise
- * returns FALSE to indicate the non-dyntopo code should run. */
+ * Returns true if this was a dynamic-topology undo step, otherwise
+ * returns false to indicate the non-dyntopo code should run. */
 static int sculpt_undo_bmesh_restore(bContext *C,
                                      SculptUndoNode *unode,
                                      Object *ob,
@@ -354,21 +375,21 @@ static int sculpt_undo_bmesh_restore(bContext *C,
        switch (unode->type) {
                case SCULPT_UNDO_DYNTOPO_BEGIN:
                        sculpt_undo_bmesh_restore_begin(C, unode, ob, ss);
-                       return TRUE;
+                       return true;
 
                case SCULPT_UNDO_DYNTOPO_END:
                        sculpt_undo_bmesh_restore_end(C, unode, ob, ss);
-                       return TRUE;
+                       return true;
 
                default:
                        if (ss->bm_log) {
-                               sculpt_undo_bmesh_restore_generic(unode, ob, ss);
-                               return TRUE;
+                               sculpt_undo_bmesh_restore_generic(C, unode, ob, ss);
+                               return true;
                        }
                        break;
        }
 
-       return FALSE;
+       return false;
 }
 
 static void sculpt_undo_restore(bContext *C, ListBase *lb)
@@ -379,21 +400,21 @@ static void sculpt_undo_restore(bContext *C, ListBase *lb)
        DerivedMesh *dm;
        SculptSession *ss = ob->sculpt;
        SculptUndoNode *unode;
-       int update = FALSE, rebuild = FALSE;
-       int need_mask = FALSE;
+       bool update = false, rebuild = false;
+       bool need_mask = false;
 
        for (unode = lb->first; unode; unode = unode->next) {
                if (strcmp(unode->idname, ob->id.name) == 0) {
                        if (unode->type == SCULPT_UNDO_MASK) {
                                /* is possible that we can't do the mask undo (below)
                                 * because of the vertex count */
-                               need_mask = TRUE;
+                               need_mask = true;
                                break;
                        }
                }
        }
 
-       sculpt_update_mesh_elements(scene, sd, ob, 0, need_mask);
+       BKE_sculpt_update_mesh_elements(scene, sd, ob, 0, need_mask);
 
        /* call _after_ sculpt_update_mesh_elements() which may update 'ob->derivedFinal' */
        dm = mesh_get_derived_final(scene, ob, 0);
@@ -422,15 +443,15 @@ static void sculpt_undo_restore(bContext *C, ListBase *lb)
                switch (unode->type) {
                        case SCULPT_UNDO_COORDS:
                                if (sculpt_undo_restore_coords(C, dm, unode))
-                                       update = TRUE;
+                                       update = true;
                                break;
                        case SCULPT_UNDO_HIDDEN:
                                if (sculpt_undo_restore_hidden(C, dm, unode))
-                                       rebuild = TRUE;
+                                       rebuild = true;
                                break;
                        case SCULPT_UNDO_MASK:
                                if (sculpt_undo_restore_mask(C, dm, unode))
-                                       update = TRUE;
+                                       update = true;
                                break;
 
                        case SCULPT_UNDO_DYNTOPO_BEGIN:
@@ -442,33 +463,37 @@ static void sculpt_undo_restore(bContext *C, ListBase *lb)
        }
 
        if (update || rebuild) {
-               int tag_update = 0;
+               bool tag_update = false;
                /* we update all nodes still, should be more clever, but also
                 * needs to work correct when exiting/entering sculpt mode and
                 * the nodes get recreated, though in that case it could do all */
                BKE_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
                BKE_pbvh_update(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw, NULL);
 
-               if (sculpt_multires_active(scene, ob)) {
+               if (BKE_sculpt_multires_active(scene, ob)) {
                        if (rebuild)
                                multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
                        else
                                multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
                }
 
-               tag_update = ((Mesh *)ob->data)->id.us > 1;
+               tag_update |= ((Mesh *)ob->data)->id.us > 1;
 
-               if (ss->modifiers_active) {
+               if (ss->kb || ss->modifiers_active) {
                        Mesh *mesh = ob->data;
                        BKE_mesh_calc_normals_tessface(mesh->mvert, mesh->totvert,
                                                       mesh->mface, mesh->totface, NULL);
 
-                       free_sculptsession_deformMats(ss);
-                       tag_update |= 1;
+                       BKE_free_sculptsession_deformMats(ss);
+                       tag_update |= true;
                }
 
-               if (tag_update)
+               if (tag_update) {
                        DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
+               }
+               else {
+                       sculpt_update_object_bounding_box(ob);
+               }
 
                /* for non-PBVH drawing, need to recreate VBOs */
                GPU_drawobject_free(ob->derivedFinal);
@@ -502,9 +527,11 @@ static void sculpt_undo_free(ListBase *lb)
                }
                if (unode->mask)
                        MEM_freeN(unode->mask);
+
                if (unode->bm_entry) {
                        BM_log_entry_drop(unode->bm_entry);
                }
+
                if (unode->bm_enter_totvert)
                        CustomData_free(&unode->bm_enter_vdata, unode->bm_enter_totvert);
                if (unode->bm_enter_totedge)
@@ -516,6 +543,23 @@ static void sculpt_undo_free(ListBase *lb)
        }
 }
 
+static bool sculpt_undo_cleanup(bContext *C, ListBase *lb)
+{
+       Object *ob = CTX_data_active_object(C);
+       SculptUndoNode *unode;
+
+       unode = lb->first;
+
+       if (unode && strcmp(unode->idname, ob->id.name) != 0) {
+               if (unode->bm_entry)
+                       BM_log_cleanup_entry(unode->bm_entry);
+
+               return true;
+       }
+
+       return false;
+}
+
 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
 {
        ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
@@ -531,7 +575,7 @@ static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh,
                                                SculptUndoNode *unode)
 {
        PBVHNode *node = unode->node;
-       BLI_bitmap *grid_hidden;
+       BLI_bitmap **grid_hidden;
        int i, *grid_indices, totgrid;
 
        grid_hidden = BKE_pbvh_grid_hidden(pbvh);
@@ -539,7 +583,7 @@ static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh,
        BKE_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid,
                                NULL, NULL, NULL, NULL);
                        
-       unode->grid_hidden = MEM_mapallocN(sizeof(BLI_bitmap) * totgrid,
+       unode->grid_hidden = MEM_mapallocN(sizeof(*unode->grid_hidden) * totgrid,
                                           "unode->grid_hidden");
                
        for (i = 0; i < totgrid; i++) {
@@ -657,7 +701,7 @@ static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
                BKE_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
                BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
                for (i = 0; i < allvert; i++) {
-                       BLI_BITMAP_MODIFY(unode->vert_hidden, i,
+                       BLI_BITMAP_SET(unode->vert_hidden, i,
                                          mvert[vert_indices[i]].flag & ME_HIDE);
                }
        }
@@ -685,11 +729,11 @@ static SculptUndoNode *sculpt_undo_bmesh_push(Object *ob,
        PBVHVertexIter vd;
 
        if (!lb->first) {
-               unode = MEM_callocN(sizeof(*unode), AT);
+               unode = MEM_callocN(sizeof(*unode), __func__);
 
                BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
                unode->type = type;
-               unode->applied = TRUE;
+               unode->applied = true;
 
                if (type == SCULPT_UNDO_DYNTOPO_END) {
                        unode->bm_entry = BM_log_entry_add(ss->bm_log);
@@ -729,15 +773,30 @@ static SculptUndoNode *sculpt_undo_bmesh_push(Object *ob,
        if (node) {
                switch (type) {
                        case SCULPT_UNDO_COORDS:
-                       case SCULPT_UNDO_HIDDEN:
                        case SCULPT_UNDO_MASK:
                                /* Before any vertex values get modified, ensure their
                                 * original positions are logged */
                                BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
-                                       BM_log_vert_before_modified(ss->bm, ss->bm_log, vd.bm_vert);
+                                       BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
+                               }
+                               BKE_pbvh_vertex_iter_end;
+                               break;
+
+                       case SCULPT_UNDO_HIDDEN:
+                       {
+                               GSetIterator gs_iter;
+                               GSet *faces = BKE_pbvh_bmesh_node_faces(node);
+                               BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
+                                       BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
                                }
                                BKE_pbvh_vertex_iter_end;
+
+                               GSET_ITER (gs_iter, faces) {
+                                       BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
+                                       BM_log_face_modified(ss->bm_log, f);
+                               }
                                break;
+                       }
 
                        case SCULPT_UNDO_DYNTOPO_BEGIN:
                        case SCULPT_UNDO_DYNTOPO_END:
@@ -819,8 +878,8 @@ SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node,
 
 void sculpt_undo_push_begin(const char *name)
 {
-       undo_paint_push_begin(UNDO_PAINT_MESH, name,
-                             sculpt_undo_restore, sculpt_undo_free);
+       ED_undo_paint_push_begin(UNDO_PAINT_MESH, name,
+                                sculpt_undo_restore, sculpt_undo_free, sculpt_undo_cleanup);
 }
 
 void sculpt_undo_push_end(void)
@@ -839,5 +898,5 @@ void sculpt_undo_push_end(void)
                        BKE_pbvh_node_layer_disp_free(unode->node);
        }
 
-       undo_paint_push_end(UNDO_PAINT_MESH);
+       ED_undo_paint_push_end(UNDO_PAINT_MESH);
 }