Flood fill for dyntopo constant detail mode.
authorAntony Riakiotakis <kalast@gmail.com>
Sat, 22 Mar 2014 15:47:18 +0000 (17:47 +0200)
committerAntony Riakiotakis <kalast@gmail.com>
Sat, 22 Mar 2014 15:47:18 +0000 (17:47 +0200)
Nothing spectacular here, fill tools are easy. Just take the dyntopo
code and repeat until nothing more to do.

The tool can be located in the dyntopo panel when the dyntopo constant
detail is on.

Also added scale factor for constant detail. This may change when detail
sampling gets in, I am not very happy with having two numbers here,
still it will give some more control for now.

release/scripts/startup/bl_ui/space_view3d_toolbar.py
source/blender/blenkernel/BKE_pbvh.h
source/blender/blenkernel/intern/pbvh_bmesh.c
source/blender/editors/sculpt_paint/sculpt.c
source/blender/makesdna/DNA_scene_types.h
source/blender/makesrna/intern/rna_sculpt_paint.c

index e39d7198b25c5e7268e233fcacbad5a0d2699c44..4dc44342fbec970ad485801846b3e80ebdc346f1 100644 (file)
@@ -1239,12 +1239,19 @@ class VIEW3D_PT_sculpt_topology(Panel, View3DPaintPanel):
         col.active = context.sculpt_object.use_dynamic_topology_sculpting
         sub = col.column(align=True)
         sub.active = brush and brush.sculpt_tool not in ('MASK')
-        sub.prop(sculpt, "detail_size")
+        if (sculpt.detail_type_method == 'CONSTANT'):
+            row = sub.row(align=True)
+            row.prop(sculpt, "detail_size")
+            row.prop(sculpt, "constant_detail_scale")
+        else:
+            sub.prop(sculpt, "detail_size")
         sub.prop(sculpt, "detail_refine_method", text="")
         sub.prop(sculpt, "detail_type_method", text="")
         col.separator()
         col.prop(sculpt, "use_smooth_shading")
         col.operator("sculpt.optimize")
+        if (sculpt.detail_type_method == 'CONSTANT'):
+           col.operator("sculpt.detail_flood_fill")
         col.separator()
         col.prop(sculpt, "symmetrize_direction")
         col.operator("sculpt.symmetrize")
index 7a0c8a1f187184e642ddf2f64173abf78e88723e..0828ea54280256c1e266e608386037f1acada4fc 100644 (file)
@@ -135,7 +135,7 @@ typedef enum {
        PBVH_Subdivide = 1,
        PBVH_Collapse = 2,
 } PBVHTopologyUpdateMode;
-int BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
+bool BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
                                    const float center[3], float radius);
 
 /* Node Access */
index 0176d2055cdece66ade090f80dfbb445f156246f..3fae446f1f4e9130dc2ad53ce937d57cc16784f0 100644 (file)
@@ -1104,7 +1104,7 @@ void BKE_pbvh_build_bmesh(PBVH *bvh, BMesh *bm, int smooth_shading,
 }
 
 /* Collapse short edges, subdivide long edges */
-int BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
+bool BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
                                    const float center[3], float radius)
 {
        /* 2 is enough for edge faces - manifold edge */
@@ -1112,7 +1112,7 @@ int BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
        BLI_buffer_declare_static(BMFace *, deleted_faces, BLI_BUFFER_NOP, 32);
        const int cd_vert_mask_offset = CustomData_get_offset(&bvh->bm->vdata, CD_PAINT_MASK);
 
-       int modified = FALSE;
+       bool modified = false;
        int n;
 
        if (mode & PBVH_Collapse) {
@@ -1122,6 +1122,7 @@ int BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
                EdgeQueueContext eq_ctx = {&q, queue_pool, bvh->bm, cd_vert_mask_offset};
 
                short_edge_queue_create(&eq_ctx, bvh, center, radius);
+               modified |= !BLI_heap_is_empty(q.heap);
                pbvh_bmesh_collapse_short_edges(&eq_ctx, bvh, &edge_loops,
                                                &deleted_faces);
                BLI_heap_free(q.heap, NULL);
@@ -1135,6 +1136,7 @@ int BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
                EdgeQueueContext eq_ctx = {&q, queue_pool, bvh->bm, cd_vert_mask_offset};
 
                long_edge_queue_create(&eq_ctx, bvh, center, radius);
+               modified |= !BLI_heap_is_empty(q.heap);
                pbvh_bmesh_subdivide_long_edges(&eq_ctx, bvh, &edge_loops);
                BLI_heap_free(q.heap, NULL);
                BLI_mempool_destroy(queue_pool);
index 7af05e7bf4a958f786b57ac287a14e9941a0fe7b..c6f8007d57e1cae9f51ddb8afdee99cc6ad65af4 100644 (file)
@@ -4510,7 +4510,8 @@ static void sculpt_stroke_update_step(bContext *C, struct PaintStroke *UNUSED(st
        sculpt_restore_mesh(sd, ob);
 
        if (sd->flags & SCULPT_DYNTOPO_DETAIL_CONSTANT) {
-               BKE_pbvh_bmesh_detail_size_set(ss->pbvh, (float)sd->detail_size / 100.0f);
+               BKE_pbvh_bmesh_detail_size_set(ss->pbvh,
+                       sd->constant_detail_scale * (float)sd->detail_size / 100.0f);
        }
        else {
                BKE_pbvh_bmesh_detail_size_set(ss->pbvh,
@@ -5164,8 +5165,12 @@ static int sculpt_mode_toggle_exec(bContext *C, wmOperator *op)
                        ts->sculpt->flags |= SCULPT_DYNTOPO_SUBDIVIDE;
                }
 
-               if (!ts->sculpt->detail_size)
+               if (!ts->sculpt->detail_size) {
                        ts->sculpt->detail_size = 30;
+               }
+
+               if (ts->sculpt->constant_detail_scale == 0.0)
+                       ts->sculpt->constant_detail_scale = 1.0f;
 
                /* Create sculpt mode session data */
                if (ob->sculpt)
@@ -5204,6 +5209,76 @@ static void SCULPT_OT_sculptmode_toggle(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
 
+
+static int sculpt_and_dynamic_topology_constant_detail_poll(bContext *C)
+{
+       Object *ob = CTX_data_active_object(C);
+       Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
+
+       return sculpt_mode_poll(C) && ob->sculpt->bm && (sd->flags & SCULPT_DYNTOPO_DETAIL_CONSTANT);
+}
+
+static int sculpt_detail_flood_fill_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
+       Object *ob = CTX_data_active_object(C);
+       SculptSession *ss = ob->sculpt;
+       float size;
+       float bb_min[3], bb_max[3];
+       int i, totnodes;
+       PBVHNode **nodes;
+
+       BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnodes);
+
+       if (!totnodes)
+               return OPERATOR_CANCELLED;
+
+       for (i = 0; i < totnodes; i++) {
+               BKE_pbvh_node_mark_topology_update(nodes[i]);
+       }
+       /* get the bounding box, store the size to bb_max and center (zero) to bb_min */
+       BKE_pbvh_bounding_box(ob->sculpt->pbvh, bb_min, bb_max);
+       sub_v3_v3(bb_max, bb_min);
+       zero_v3(bb_min);
+       size = max_fff(bb_max[0], bb_max[1], bb_max[2]);
+
+       /* update topology size */
+       BKE_pbvh_bmesh_detail_size_set(ss->pbvh,
+                       sd->constant_detail_scale * (float)sd->detail_size / 100.0f);
+
+       sculpt_undo_push_begin("Dynamic topology flood fill");
+       sculpt_undo_push_node(ob, NULL, SCULPT_UNDO_COORDS);
+
+       while (BKE_pbvh_bmesh_update_topology(ss->pbvh, PBVH_Collapse | PBVH_Subdivide, bb_min, size)) {
+               for (i = 0; i < totnodes; i++)
+                       BKE_pbvh_node_mark_topology_update(nodes[i]);
+       }
+
+       MEM_freeN(nodes);
+       sculpt_undo_push_end();
+
+       /* force rebuild of pbvh for better BB placement */
+       sculpt_pbvh_clear(ob);
+       /* Redraw */
+       WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
+
+       return OPERATOR_FINISHED;
+}
+
+static void SCULPT_OT_detail_flood_fill(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Flood Fill";
+       ot->idname = "SCULPT_OT_detail_flood_fill";
+       ot->description = "Flood fill the mesh with the selected detail setting";
+
+       /* api callbacks */
+       ot->exec = sculpt_detail_flood_fill_exec;
+       ot->poll = sculpt_and_dynamic_topology_constant_detail_poll;
+
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+}
+
 void ED_operatortypes_sculpt(void)
 {
        WM_operatortype_append(SCULPT_OT_brush_stroke);
@@ -5212,4 +5287,5 @@ void ED_operatortypes_sculpt(void)
        WM_operatortype_append(SCULPT_OT_dynamic_topology_toggle);
        WM_operatortype_append(SCULPT_OT_optimize);
        WM_operatortype_append(SCULPT_OT_symmetrize);
+       WM_operatortype_append(SCULPT_OT_detail_flood_fill);
 }
index d66a1f3ab0b28dbf750a527fdb3f507993c208fc..02b213a1b683a677b913cd8c2265105ca8b5d9ed 100644 (file)
@@ -855,7 +855,9 @@ typedef struct Sculpt {
 
        /* gravity factor for sculpting */
        float gravity_factor;
-       int pad;
+
+       /* scale for constant detail size */
+       float constant_detail_scale;
 
        struct Object *gravity_object;
        void *pad2;
index 635c9a3c374f23b9a3c28a32dc5b435dacc44fdc..77cad9affb72d53662a17ada9f809e6d65bd03d7 100644 (file)
@@ -416,7 +416,13 @@ static void rna_def_sculpt(BlenderRNA  *brna)
 
        prop = RNA_def_property(srna, "detail_size", PROP_INT, PROP_NONE);
        RNA_def_property_ui_range(prop, 2, 100, 0, -1);
-       RNA_def_property_ui_text(prop, "Detail Size", "Maximum edge length for dynamic topology sculpting (in pixels)");
+       RNA_def_property_ui_text(prop, "Detail Size", "Maximum edge length for dynamic topology sculpting");
+       RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL);
+
+       prop = RNA_def_property(srna, "constant_detail_scale", PROP_FLOAT, PROP_NONE);
+       RNA_def_property_range(prop, 0.001, 100.0);
+       RNA_def_property_ui_range(prop, 0.1, 100.0, 10, 2);
+       RNA_def_property_ui_text(prop, "Scale", "Multiplier for constant detail size");
        RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL);
 
        prop = RNA_def_property(srna, "use_smooth_shading", PROP_BOOLEAN, PROP_NONE);