[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [18286] branches/bmesh/bmesh: Refactored the way operators are defined, to make it

Joseph Eagar joeedh at gmail.com
Sat Jan 3 08:38:41 CET 2009


Revision: 18286
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=18286
Author:   joeedh
Date:     2009-01-03 08:38:40 +0100 (Sat, 03 Jan 2009)

Log Message:
-----------
Refactored the way operators are defined, to make it
less messy, confusing and easier to maintain.

Also added a function to build a pointer array from
elements that share a certain flag (like
selection), and another function that counts the
number of elements with that flag.  Still need to
make use of them properly though.

Modified Paths:
--------------
    branches/bmesh/bmesh/bmesh_marking.h
    branches/bmesh/bmesh/bmesh_operators.h
    branches/bmesh/bmesh/intern/bmesh_marking.c
    branches/bmesh/bmesh/intern/bmesh_mods.c
    branches/bmesh/bmesh/intern/bmesh_operators.c
    branches/bmesh/bmesh/intern/bmesh_operators_private.h
    branches/bmesh/bmesh/operators/bmesh_dupeops.c

Added Paths:
-----------
    branches/bmesh/bmesh/intern/bmesh_opdefines.c

Modified: branches/bmesh/bmesh/bmesh_marking.h
===================================================================
--- branches/bmesh/bmesh/bmesh_marking.h	2009-01-03 07:25:22 UTC (rev 18285)
+++ branches/bmesh/bmesh/bmesh_marking.h	2009-01-03 07:38:40 UTC (rev 18286)
@@ -7,6 +7,9 @@
 void BM_Select_Face(struct BMesh *bm, struct BMFace *f, int select);
 void BM_Selectmode_Set(struct BMesh *bm, int selectmode);
 
+/*counts number of elements with flag set*/
+int BM_CountFlag(struct BMesh *bm, int type, int flag);
+
 void BM_Select(struct BMesh *bm, void *element, int select);
 int BM_Is_Selected(BMesh *bm, void *element);
 

Modified: branches/bmesh/bmesh/bmesh_operators.h
===================================================================
--- branches/bmesh/bmesh/bmesh_operators.h	2009-01-03 07:25:22 UTC (rev 18285)
+++ branches/bmesh/bmesh/bmesh_operators.h	2009-01-03 07:38:40 UTC (rev 18286)
@@ -47,10 +47,15 @@
 /*need to refactor code to use this*/
 typedef struct BMOpDefine {
 	int slottypes[BMOP_MAX_SLOTS];
-	int totslots;
+	void (*exec)(BMesh *bm, BMOperator *op);
+	int totslot;
 	int flag;
 } BMOpDefine;
 
+/*BMOpDefine->flag*/
+//doesn't do anything at the moment.
+#define BMO_NOFLAGS		1
+
 /*API for operators*/
 void BMO_Init_Op(struct BMOperator *op, int opcode);
 void BMO_Exec_Op(struct BMesh *bm, struct BMOperator *op);
@@ -72,45 +77,39 @@
 /*--------------------begin operator defines------------------------*/
 /*split op*/
 #define BMOP_SPLIT				0
-#define BMOP_SPLIT_MULTIN		0
-#define BMOP_SPLIT_MULTOUT		1
-#define BMOP_SPLIT_TOTSLOT		2
 
-static const int BMOP_SPLIT_TYPEINFO[BMOP_SPLIT_TOTSLOT] = {
-	BMOP_OPSLOT_PNT_BUF,
-	BMOP_OPSLOT_PNT_BUF
+enum {
+	BMOP_SPLIT_MULTIN,
+	BMOP_SPLIT_MULTOUT,
+	BMOP_SPLIT_TOTSLOT,
 };
 
 /*dupe op*/
-#define BMOP_DUPE				1
+#define BMOP_DUPE	1
 
-#define BMOP_DUPE_MULTIN		0
-#define BMOP_DUPE_ORIG			1
-#define BMOP_DUPE_NEW			2
-#define BMOP_DUPE_TOTSLOT		3
-
-static const int BMOP_DUPE_TYPEINFO[BMOP_DUPE_TOTSLOT] = {
-	BMOP_OPSLOT_PNT_BUF,
-	BMOP_OPSLOT_PNT_BUF,
-	BMOP_OPSLOT_PNT_BUF,
+enum {
+	BMOP_DUPE_MULTIN,
+	BMOP_DUPE_ORIG,
+	BMOP_DUPE_NEW,
+	BMOP_DUPE_TOTSLOT
 };
 
 /*delete op*/
-#define BMOP_DEL			2
-#define BMOP_DEL_MULTIN		0
-#define BMOP_DEL_CONTEXT	1
-#define BMOP_DEL_TOTSLOT	2
+#define BMOP_DEL	2
 
-/*BMOP_DEL_CONTEXT*/
-#define BMOP_DEL_VERTS				1
-#define BMOP_DEL_EDGESFACES			2
-#define BMOP_DEL_ONLYFACES			3
-#define BMOP_DEL_FACES				4
-#define BMOP_DEL_ALL				5
+enum {
+	BMOP_DEL_MULTIN,
+	BMOP_DEL_CONTEXT,
+	BMOP_DEL_TOTSLOT,
+};
 
-static const int BMOP_DEL_TYPEINFO[BMOP_DEL_TOTSLOT] = {
-	BMOP_OPSLOT_PNT_BUF,
-	BMOP_OPSLOT_INT
+/*BMOP_DEL_CONTEXT*/
+enum {
+	BMOP_DEL_VERTS = 1,
+	BMOP_DEL_EDGESFACES,
+	BMOP_DEL_ONLYFACES,
+	BMOP_DEL_FACES,
+	BMOP_DEL_ALL,
 };
 
 /*editmesh->bmesh op*/
@@ -118,51 +117,21 @@
 #define BMOP_FROM_EDITMESH_EM	0
 #define BMOP_FROM_EDITMESH_TOTSLOT 1
 
-static const int BMOP_FROM_EDITMESH_TYPEINFO[BMOP_FROM_EDITMESH_TOTSLOT] = {
-	BMOP_OPSLOT_PNT
-};
-
-#define FROMEDIT_OPTIONS		NEEDFLAGS
-
 /*bmesh->editmesh op*/
 #define BMOP_TO_EDITMESH		4
 #define BMOP_TO_EDITMESH_EM		0
 #define BMOP_TO_EDITMESH_TOTSLOT 1
 
-static const int BMOP_TO_EDITMESH_TYPEINFO[BMOP_TO_EDITMESH_TOTSLOT] = {
-	BMOP_OPSLOT_PNT
-};
-
-
+/*edge subdivide op*/
 #define BMOP_ESUBDIVIDE			5
 #define BMOP_ESUBDIVIDE_EDGES	0
 #define BMOP_ESUBDIVIDE_TOTSLOT	1
 
-static const int BMOP_ESUBDIVIDE_TYPEINFO[] = {
-	BMOP_OPSLOT_PNT_BUF
-};
-
 /*keep this updated!*/
 #define BMOP_TOTAL_OPS				6
 /*-------------------------------end operator defines-------------------------------*/
 
-/*Following arrays are used by the init functions to init operator slot typecodes*/
-static const int *BMOP_TYPEINFO[] = {
-	BMOP_SPLIT_TYPEINFO,
-	BMOP_DUPE_TYPEINFO,
-	BMOP_DEL_TYPEINFO,
-	BMOP_FROM_EDITMESH_TYPEINFO,
-	BMOP_TO_EDITMESH_TYPEINFO,
-	BMOP_ESUBDIVIDE_TYPEINFO
-};
+extern BMOpDefine *opdefines[];
+extern int bmesh_total_ops;
 
-static const int BMOP_TYPETOTALS[] = {
-	BMOP_SPLIT_TOTSLOT,
-	BMOP_DUPE_TOTSLOT,
-	BMOP_DEL_TOTSLOT,
-	BMOP_FROM_EDITMESH_TOTSLOT,
-	BMOP_TO_EDITMESH_TOTSLOT,
-	BMOP_ESUBDIVIDE_TOTSLOT
-};
-
 #endif

Modified: branches/bmesh/bmesh/intern/bmesh_marking.c
===================================================================
--- branches/bmesh/bmesh/intern/bmesh_marking.c	2009-01-03 07:25:22 UTC (rev 18285)
+++ branches/bmesh/bmesh/intern/bmesh_marking.c	2009-01-03 07:38:40 UTC (rev 18286)
@@ -200,6 +200,31 @@
 }
 
 
+int BM_CountFlag(struct BMesh *bm, int type, int flag)
+{
+	BMHeader *head;
+	BMIter iter;
+	int tot = 0;
+
+	if (type & BM_VERT) {
+		for (head = BMIter_New(&iter, bm, BM_VERTS, NULL); head; head=BMIter_Step(&iter)) {
+			if (head->flag & flag) tot++;
+		}
+	}
+	if (type & BM_EDGE) {
+		for (head = BMIter_New(&iter, bm, BM_EDGES, NULL); head; head=BMIter_Step(&iter)) {
+			if (head->flag & flag) tot++;
+		}
+	}
+	if (type & BM_FACE) {
+		for (head = BMIter_New(&iter, bm, BM_FACES, NULL); head; head=BMIter_Step(&iter)) {
+			if (head->flag & flag) tot++;
+		}
+	}
+
+	return tot;
+}
+
 void BM_Select(struct BMesh *bm, void *element, int select)
 {
 	BMHeader *head = element;

Modified: branches/bmesh/bmesh/intern/bmesh_mods.c
===================================================================
--- branches/bmesh/bmesh/intern/bmesh_mods.c	2009-01-03 07:25:22 UTC (rev 18285)
+++ branches/bmesh/bmesh/intern/bmesh_mods.c	2009-01-03 07:38:40 UTC (rev 18286)
@@ -124,13 +124,22 @@
 	return bmesh_jfke(bm, f1, f2, jed);
 }
 
+/*connects two verts together, automatically (if very naively) finding the
+  face they both share (if there is one) and splittling it.  use this at your 
+  own risk, as it doesn't handle the many complex cases it should (like zero-area faces,
+  multiple faces, etc).
+
+  this is really only meant for cases where you don't know before hand the face
+  the two verts belong to for splitting (e.g. the subdivision operator).
+*/
+
 BMEdge *BM_Connect_Verts(BMesh *bm, BMVert *v1, BMVert *v2, BMFace **nf) {
 	/*search radial disk for face that contains e1 and e2*/
 	BMIter iter, iter2;
 	BMVert *v;
 	BMLoop *nl;
 	BMFace *face;
-	
+
 	/*this isn't the best thing in the world.  it doesn't handle cases where there's
 	  multiple faces yet.  that might require a convexity test to figure out which
 	  face is "best," and who knows what for non-manifold conditions.*/

Added: branches/bmesh/bmesh/intern/bmesh_opdefines.c
===================================================================
--- branches/bmesh/bmesh/intern/bmesh_opdefines.c	                        (rev 0)
+++ branches/bmesh/bmesh/intern/bmesh_opdefines.c	2009-01-03 07:38:40 UTC (rev 18286)
@@ -0,0 +1,57 @@
+#include "bmesh.h"
+#include "bmesh_private.h"
+
+#include <stdio.h>
+
+BMOpDefine def_subdop = {
+	{BMOP_OPSLOT_PNT_BUF},
+	esubdivide_exec,
+	BMOP_ESUBDIVIDE_TOTSLOT,
+	0
+};
+
+BMOpDefine def_edit2bmesh = {
+	{BMOP_OPSLOT_PNT},
+	NULL,
+	BMOP_TO_EDITMESH_TOTSLOT,
+	0
+};
+
+BMOpDefine def_bmesh2edit = {
+	{BMOP_OPSLOT_PNT},
+	NULL,
+	BMOP_FROM_EDITMESH_TOTSLOT,
+	0
+};
+
+BMOpDefine def_delop = {
+	{BMOP_OPSLOT_PNT_BUF, BMOP_OPSLOT_INT},
+	delop_exec,
+	BMOP_DEL_TOTSLOT,
+	0
+};
+
+BMOpDefine def_dupeop = {
+	{BMOP_OPSLOT_PNT_BUF, BMOP_OPSLOT_PNT_BUF, BMOP_OPSLOT_PNT_BUF},
+	dupeop_exec,
+	BMOP_DUPE_TOTSLOT,
+	0
+};
+
+BMOpDefine def_splitop = {
+	{BMOP_OPSLOT_PNT_BUF, BMOP_OPSLOT_PNT_BUF},
+	splitop_exec,
+	BMOP_SPLIT_TOTSLOT,
+	0
+};
+
+BMOpDefine *opdefines[] = {
+	&def_splitop,
+	&def_dupeop,
+	&def_delop,
+	&def_edit2bmesh,
+	&def_bmesh2edit,
+	&def_subdop,
+};
+
+int bmesh_total_ops = (sizeof(opdefines) / sizeof(void*));

Modified: branches/bmesh/bmesh/intern/bmesh_operators.c
===================================================================
--- branches/bmesh/bmesh/intern/bmesh_operators.c	2009-01-03 07:25:22 UTC (rev 18285)
+++ branches/bmesh/bmesh/intern/bmesh_operators.c	2009-01-03 07:38:40 UTC (rev 18286)
@@ -14,18 +14,8 @@
 static void alloc_flag_layer(BMesh *bm);
 static void free_flag_layer(BMesh *bm);
 
-void esubdivide_exec(BMesh *bmesh, BMOperator *op);
-
 /*function pointer table*/
 typedef void (*opexec)(struct BMesh *bm, struct BMOperator *op);
-const opexec BMOP_OPEXEC[BMOP_TOTAL_OPS] = {
-	splitop_exec,
-	dupeop_exec,
-	delop_exec,
-	NULL,
-	NULL,
-	esubdivide_exec,
-};
 
 /*operator slot type information - size of one element of the type given.*/
 const int BMOP_OPSLOT_TYPEINFO[BMOP_OPSLOT_TYPES] = {
@@ -86,17 +76,18 @@
 
 	memset(op, 0, sizeof(BMOperator));
 	op->type = opcode;
+	
+	//currently not used, flags are always allocated
+	op->needflag = !(opdefines[opcode]->flag & BMO_NOFLAGS);
 
-	if (BMOP_OPTIONS[opcode] & NEEDFLAGS) op->needflag = 1;
-
 	/*initialize the operator slot types*/
-	for(i = 0; i < BMOP_TYPETOTALS[opcode]; i++) {
-		op->slots[i].slottype = BMOP_TYPEINFO[opcode][i];
+	for(i = 0; i < opdefines[opcode]->totslot; i++) {
+		op->slots[i].slottype = opdefines[opcode]->slottypes[i];
 		op->slots[i].index = i;
 	}
 
 	/*callback*/
-	op->exec = BMOP_OPEXEC[opcode];
+	op->exec = opdefines[opcode]->exec;
 
 	/*memarena, used for operator's slot buffers*/
 	op->arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
@@ -178,8 +169,8 @@
 		dest_slot->data.buf = NULL;
 		dest_slot->len = source_slot->len;
 		if(dest_slot->len){
-			dest_slot->data.buf = BLI_memarena_alloc(dest_op->arena, BMOP_TYPETOTALS[dest_slot->slottype] * dest_slot->len);
-			memcpy(dest_slot->data.buf, source_slot->data.buf, BMOP_TYPETOTALS[dest_slot->slottype] * dest_slot->len);
+			dest_slot->data.buf = BLI_memarena_alloc(dest_op->arena, opdefines[dest_slot->slottype]->totslot * dest_slot->len);
+			memcpy(dest_slot->data.buf, source_slot->data.buf, opdefines[dest_slot->slottype]->totslot * dest_slot->len);
 		}
 	} else {
 		dest_slot->data = source_slot->data;
@@ -318,7 +309,7 @@
 	
 	op->slots[slotcode].len = len;
 	if(len)
-		op->slots[slotcode].data.buf = BLI_memarena_alloc(op->arena, BMOP_TYPEINFO[op->type][slotcode] * len);
+		op->slots[slotcode].data.buf = BLI_memarena_alloc(op->arena, opdefines[op->type]->slottypes[slotcode] * len);
 	return op->slots[slotcode].data.buf;
 }
 
@@ -331,6 +322,47 @@
  *
 */
 
+void BMO_HeaderFlag_To_Slot(BMesh *bm, BMOperator *op, int slotcode, int flag, int type)
+{
+	BMIter elements;
+	BMHeader *e;
+	BMOpSlot *output = BMO_GetSlot(op, slotcode);
+	int totelement=0, i=0;
+	
+	totelement = BM_CountFlag(bm, type, BM_SELECT);
+
+	if(totelement){
+		alloc_slot_buffer(op, slotcode, totelement);
+
+		if (type & BM_VERT) {
+			for (e = BMIter_New(&elements, bm, BM_VERTS, bm); e; e = BMIter_Step(&elements)) {

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list