patch [#34890] BMesh Poke Face.
authorCampbell Barton <ideasman42@gmail.com>
Sat, 6 Apr 2013 02:45:43 +0000 (02:45 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 6 Apr 2013 02:45:43 +0000 (02:45 +0000)
by Francisco De La Cruz (xercesblue), with some of my own changes/improvements.

Converts faces to triangle-fans (useful to run on ngons).

To access select a group of faces and press "Alt+P" or alternatively select the operator from the Faces menu (Ctrl+F)

release/scripts/startup/bl_ui/space_view3d.py
source/blender/bmesh/CMakeLists.txt
source/blender/bmesh/intern/bmesh_opdefines.c
source/blender/bmesh/intern/bmesh_operators.h
source/blender/bmesh/intern/bmesh_operators_private.h
source/blender/bmesh/operators/bmo_poke.c [new file with mode: 0644]
source/blender/editors/mesh/editmesh_tools.c
source/blender/editors/mesh/mesh_intern.h
source/blender/editors/mesh/mesh_ops.c

index d3350a77ba7c476870aeaa45904496f5b323f440..abfbe7efaa9a81fdea2511d42be43178d7282703 100644 (file)
@@ -1946,6 +1946,7 @@ class VIEW3D_MT_edit_mesh_faces(Menu):
 
         layout.separator()
 
+        layout.operator("mesh.poke")
         layout.operator("mesh.quads_convert_to_tris")
         layout.operator("mesh.tris_convert_to_quads")
 
index 4c2115b5052d931f64aaff5caa1dab01d51e5b12..b0e78a43337089bbd4dfc38f9751c0bc0b3bbf0d 100644 (file)
@@ -54,6 +54,7 @@ set(SRC
        operators/bmo_join_triangles.c
        operators/bmo_mesh_conv.c
        operators/bmo_mirror.c
+       operators/bmo_poke.c
        operators/bmo_primitive.c
        operators/bmo_removedoubles.c
        operators/bmo_similar.c
index c76bf15022c05bf689fa2e8021cd0b17ff17a16a..83f57baea184ec2ad8ae38c8938b5846fc6cfee4 100644 (file)
@@ -1571,6 +1571,28 @@ static BMOpDefine bmo_wireframe_def = {
        0
 };
 
+/*
+ * Pokes a face.
+ *
+ * Splits a face into a triangle fan.
+ */
+static BMOpDefine bmo_poke_def = {
+       "poke",
+       /* slots_in */
+       {{"faces", BMO_OP_SLOT_ELEMENT_BUF, {BM_FACE}},   /* input faces */
+        {"offset", BMO_OP_SLOT_FLT}, /* center vertex offset along normal */
+       {"center_mode", BMO_OP_SLOT_INT}, /* calculation mode for center vertex */
+        {{'\0'}},
+       },
+       /* slots_out */
+       {{"verts.out", BMO_OP_SLOT_ELEMENT_BUF, {BM_VERT}}, /* output verts */
+        {"faces.out", BMO_OP_SLOT_ELEMENT_BUF, {BM_FACE}}, /* output faces */
+        {{'\0'}},
+       },
+       bmo_poke_exec,
+       0
+};
+
 #ifdef WITH_BULLET
 /*
  * Convex Hull
@@ -1677,6 +1699,7 @@ const BMOpDefine *bmo_opdefines[] = {
        &bmo_object_load_bmesh_def,
        &bmo_pointmerge_def,
        &bmo_pointmerge_facedata_def,
+       &bmo_poke_def,
        &bmo_recalc_face_normals_def,
        &bmo_region_extend_def,
        &bmo_remove_doubles_def,
index d02b0dce728d35479c827ad1df3c1eb9bf3f8e90..2063ab49169128d440bf95a7603a2474ae8bdbcc 100644 (file)
@@ -89,6 +89,13 @@ enum {
        VPATH_SELECT_TOPOLOGICAL
 };
 
+/* Poke face center calculation */
+enum {
+       BMOP_POKE_MEAN_WEIGHTED = 0,
+       BMOP_POKE_MEAN,
+       BMOP_POKE_BOUNDS
+};
+
 extern const BMOpDefine *bmo_opdefines[];
 extern const int         bmo_opdefines_total;
 
index 6f9628bc468a9abfe5a5975e1a50c511125f888f..2a67407b261003513ad896073e0786e495ad15c7 100644 (file)
@@ -74,6 +74,7 @@ void bmo_object_load_bmesh_exec(BMesh *bm, BMOperator *op);
 void bmo_pointmerge_exec(BMesh *bm, BMOperator *op);
 void bmo_pointmerge_facedata_exec(BMesh *bm, BMOperator *op);
 void bmo_recalc_face_normals_exec(BMesh *bm, BMOperator *op);
+void bmo_poke_exec(BMesh *bm, BMOperator *op);
 void bmo_region_extend_exec(BMesh *bm, BMOperator *op);
 void bmo_remove_doubles_exec(BMesh *bm, BMOperator *op);
 void bmo_reverse_colors_exec(BMesh *bm, BMOperator *op);
diff --git a/source/blender/bmesh/operators/bmo_poke.c b/source/blender/bmesh/operators/bmo_poke.c
new file mode 100644 (file)
index 0000000..939bfb3
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Francisco De La Cruz
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/bmesh/operators/bmo_poke.c
+ *  \ingroup bmesh
+ *
+ * Pokes a face.
+ *
+ * Splits a face into a triangle fan.
+ */
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_math.h"
+#include "BLI_array.h"
+
+#include "bmesh.h"
+
+#include "intern/bmesh_operators_private.h" /* own include */
+
+#define ELE_NEW 1
+
+/**
+ * Pokes a face
+ *
+ * Splits a face into a triangle fan.
+ * Iterate over all selected faces, create a new center vertex and
+ * create triangles between original face edges and new center vertex.
+ */
+void bmo_poke_exec(BMesh *bm, BMOperator *op)
+{
+       BMOIter oiter;
+       BMFace *f;
+
+       const float offset = BMO_slot_float_get(op->slots_in, "offset");
+       const int center_mode = BMO_slot_int_get(op->slots_in, "center_mode");
+
+       void (*bm_face_calc_center_fn)(BMFace *f, float r_cent[3]);
+
+       switch (center_mode) {
+               case BMOP_POKE_MEAN_WEIGHTED:
+                       bm_face_calc_center_fn = BM_face_calc_center_mean_weighted;
+                       break;
+               case BMOP_POKE_BOUNDS:
+                       bm_face_calc_center_fn = BM_face_calc_center_bounds;
+                       break;
+               case BMOP_POKE_MEAN:
+                       bm_face_calc_center_fn = BM_face_calc_center_mean;
+                       break;
+               default:
+                       BLI_assert(0);
+                       break;
+       }
+
+       BMO_ITER(f, &oiter, op->slots_in, "faces", BM_FACE) {
+               BMFace *f_new;
+               float f_center[3];
+               BMVert *v_center = NULL;
+               BMLoop *l_iter, *l_first;
+               /* only interpolate the centeral loop from the face once,
+                * then copy to all others in the fan */
+               BMLoop *l_center_example;
+
+               int i;
+
+               bm_face_calc_center_fn(f, f_center);
+               v_center = BM_vert_create(bm, f_center, NULL, 0);
+               BMO_elem_flag_enable(bm, v_center, ELE_NEW);
+
+               /* handled by BM_loop_interp_from_face */
+               // BM_vert_interp_from_face(bm, v_center, f);
+
+               i = 0;
+               l_iter = l_first = BM_FACE_FIRST_LOOP(f);
+               do {
+                       BMLoop *l_new;
+
+                       f_new = BM_face_create_quad_tri(bm, l_iter->v, l_iter->next->v, v_center, NULL, f, false);
+                       l_new = BM_FACE_FIRST_LOOP(f_new);
+
+                       if (i == 0) {
+                               l_center_example = l_new->prev;
+                               BM_loop_interp_from_face(bm, l_center_example, f, true, true);
+                       }
+                       else {
+                               BM_elem_attrs_copy(bm, bm, l_center_example, l_new->prev);
+                       }
+
+                       /* Copy Loop Data */
+                       BM_elem_attrs_copy(bm, bm, l_iter, l_new);
+                       BM_elem_attrs_copy(bm, bm, l_iter->next, l_new->next);
+
+                       BMO_elem_flag_enable(bm, f_new, ELE_NEW);
+               } while (i++, (l_iter = l_iter->next) != l_first);
+
+               copy_v3_v3(v_center->no, f->no);
+               madd_v3_v3fl(v_center->co, v_center->no, offset);
+
+               /* Kill Face */
+               BM_face_kill(bm, f);
+       }
+
+       BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "verts.out", BM_VERT, ELE_NEW);
+       BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, ELE_NEW);
+}
index a7264d3b006e547c216db35861cc2a6f90375632..2dbe28523033352f8683b9cf58373ea4f95bf414 100644 (file)
@@ -2631,6 +2631,37 @@ void MESH_OT_beautify_fill(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
 
+
+/********************** Poke Face **********************/
+
+static int edbm_poke_face_exec(bContext *C, wmOperator *op)
+{
+       Object *obedit = CTX_data_edit_object(C);
+       BMEditMesh *em = BMEdit_FromObject(obedit);
+       BMOperator bmop;
+
+       const float offset = RNA_float_get(op->ptr, "offset");
+       const int center_mode = RNA_enum_get(op->ptr, "center_mode");
+
+       EDBM_op_init(em, &bmop, op, "poke faces=%hf offset=%f center_mode=%i", BM_ELEM_SELECT, offset, center_mode);
+       BMO_op_exec(em->bm, &bmop);
+
+       EDBM_flag_disable_all(em, BM_ELEM_SELECT);
+
+       BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "verts.out", BM_VERT, BM_ELEM_SELECT, true);
+       BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "faces.out", BM_FACE, BM_ELEM_SELECT, true);
+
+       if (!EDBM_op_finish(em, &bmop, op, true)) {
+               return OPERATOR_CANCELLED;
+       }
+
+       EDBM_mesh_normals_update(em);
+
+       EDBM_update_generic(em, true, true);
+
+       return OPERATOR_FINISHED;
+
+}
 /********************** Quad/Tri Operators *************************/
 
 static int edbm_quads_convert_to_tris_exec(bContext *C, wmOperator *op)
@@ -2659,6 +2690,32 @@ static int edbm_quads_convert_to_tris_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
+void MESH_OT_poke(wmOperatorType *ot)
+{
+
+       static EnumPropertyItem poke_center_modes[] = {
+               {BMOP_POKE_MEAN_WEIGHTED, "MEAN_WEIGHTED", 0, "Weighted Mean", "Weighted Mean Face Center"},
+               {BMOP_POKE_MEAN, "MEAN", 0, "Mean", "Mean Face Center"},
+               {BMOP_POKE_BOUNDS, "BOUNDS", 0, "Bounds", "Face Bounds Center"},
+               {0, NULL, 0, NULL, NULL}};
+
+
+       /* identifiers */
+       ot->name = "Poke Faces";
+       ot->idname = "MESH_OT_poke";
+       ot->description = "Splits a face into a fan";
+
+       /* api callbacks */
+       ot->exec = edbm_poke_face_exec;
+       ot->poll = ED_operator_editmesh;
+
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+       RNA_def_float(ot->srna, "offset", 0.0f, -FLT_MAX, FLT_MAX, "Poke Offset", "Poke Offset", -1.0f, 1.0f);
+       RNA_def_enum(ot->srna, "center_mode", poke_center_modes, BMOP_POKE_MEAN_WEIGHTED, "Poke Center", "Poke Face Center Calculation");
+}
+
 void MESH_OT_quads_convert_to_tris(wmOperatorType *ot)
 {
        /* identifiers */
index 996ee9fc549c3d52b70905bfcd40b1de3870d112..937fbf8146bfd86aa15f42dad170b4451f34940c 100644 (file)
@@ -202,6 +202,7 @@ void MESH_OT_edge_face_add(struct wmOperatorType *ot);
 void MESH_OT_duplicate(struct wmOperatorType *ot);
 void MESH_OT_merge(struct wmOperatorType *ot);
 void MESH_OT_remove_doubles(struct wmOperatorType *ot);
+void MESH_OT_poke(struct wmOperatorType *ot);
 
 
 /* *** mesh_data.c *** */
index a79923a4fdbe48c05011d688fed45dded1fe505b..db6f6e3aeb5c1f8ad7b2e00a939f09ee215cbc28 100644 (file)
@@ -152,6 +152,7 @@ void ED_operatortypes_mesh(void)
 
        WM_operatortype_append(MESH_OT_bridge_edge_loops);
        WM_operatortype_append(MESH_OT_inset);
+       WM_operatortype_append(MESH_OT_poke);
        WM_operatortype_append(MESH_OT_wireframe);
        WM_operatortype_append(MESH_OT_edge_split);
 
@@ -260,7 +261,7 @@ void ED_keymap_mesh(wmKeyConfig *keyconf)
        
        WM_keymap_add_item(keymap, "MESH_OT_loopcut_slide", RKEY, KM_PRESS, KM_CTRL, 0);        
        WM_keymap_add_item(keymap, "MESH_OT_inset", IKEY, KM_PRESS, 0, 0);
-
+       WM_keymap_add_item(keymap, "MESH_OT_poke", PKEY, KM_PRESS, KM_ALT, 0);
        kmi = WM_keymap_add_item(keymap, "MESH_OT_bevel", BKEY, KM_PRESS, KM_CTRL, 0);
        RNA_boolean_set(kmi->ptr, "vertex_only", false);
        kmi = WM_keymap_add_item(keymap, "MESH_OT_bevel", BKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);