bmesh code cleanup

* change BMO_elem_flag_* defines to inline functions.
* BMO_slot_map_insert() is too big for an inline function - un-inline it.
* remove redundant casts.
This commit is contained in:
Campbell Barton 2012-02-25 20:58:03 +00:00
parent 4f4bba39fb
commit 98aececc8e
18 changed files with 145 additions and 94 deletions

@ -170,23 +170,6 @@ void BMO_op_exec(struct BMesh *bm, struct BMOperator *op);
* after it finishes executing in BMO_op_exec).*/
void BMO_op_finish(struct BMesh *bm, struct BMOperator *op);
/* tool flag API. never, ever ever should tool code put junk in
* header flags (element->head.flag), nor should they use
* element->head.eflag1/eflag2. instead, use this api to set
* flags.
*
* if you need to store a value per element, use a
* ghash or a mapping slot to do it. */
/* flags 15 and 16 (1<<14 and 1<<15) are reserved for bmesh api use */
#define BMO_elem_flag_test(bm, element, oflag) ((element)->oflags[bm->stackdepth-1].f & (oflag))
#define BMO_elem_flag_enable(bm, element, oflag) ((element)->oflags[bm->stackdepth-1].f |= (oflag))
#define BMO_elem_flag_disable(bm, element, oflag) ((element)->oflags[bm->stackdepth-1].f &= ~(oflag))
#define BMO_elem_flag_set(bm, element, oflag, val)((val) ? BMO_elem_flag_enable(bm, element, oflag) : \
BMO_elem_flag_disable(bm, element, oflag))
#define BMO_elem_flag_toggle(bm, element, oflag) ((element)->oflags[bm->stackdepth-1].f ^= (oflag))
/* count the number of elements with a specific flag.
* type can be a bitmask of BM_FACE, BM_EDGE, or BM_FACE. */
int BMO_mesh_flag_count(struct BMesh *bm, const short oflag, const char htype);
@ -331,6 +314,9 @@ void BMO_slot_from_hflag(struct BMesh *bm, struct BMOperator *op, const char *sl
int BMO_slot_buf_count(struct BMesh *bm, struct BMOperator *op, const char *slotname);
int BMO_slot_map_count(struct BMesh *bm, struct BMOperator *op, const char *slotname);
void BMO_slot_map_insert(BMesh *UNUSED(bm), BMOperator *op, const char *slotname,
void *element, void *data, int len);
/* Counts the number of edges with tool flag toolflag around
*/
int BMO_vert_edge_flags_count(BMesh *bm, BMVert *v, const short oflag);

@ -559,25 +559,37 @@ void BM_elem_attrs_copy(BMesh *source_mesh, BMesh *target_mesh, const void *sour
{
const BMHeader *sheader = source;
BMHeader *theader = target;
BLI_assert(sheader->htype == theader->htype);
if (sheader->htype != theader->htype)
return;
/* First we copy select */
if (BM_elem_flag_test(source, BM_ELEM_SELECT)) BM_elem_select_set(target_mesh, target, TRUE);
if (BM_elem_flag_test(source, BM_ELEM_SELECT)) {
BM_elem_select_set(target_mesh, target, TRUE);
}
/* Now we copy flags */
theader->hflag = sheader->hflag;
/* Copy specific attributes */
if (theader->htype == BM_VERT)
bm_vert_attrs_copy(source_mesh, target_mesh, (const BMVert *)source, (BMVert *)target);
else if (theader->htype == BM_EDGE)
bm_edge_attrs_copy(source_mesh, target_mesh, (const BMEdge *)source, (BMEdge *)target);
else if (theader->htype == BM_LOOP)
bm_loop_attrs_copy(source_mesh, target_mesh, (const BMLoop *)source, (BMLoop *)target);
else if (theader->htype == BM_FACE)
bm_face_attrs_copy(source_mesh, target_mesh, (const BMFace *)source, (BMFace *)target);
switch (theader->htype) {
case BM_VERT:
bm_vert_attrs_copy(source_mesh, target_mesh, (const BMVert *)source, (BMVert *)target);
break;
case BM_EDGE:
bm_edge_attrs_copy(source_mesh, target_mesh, (const BMEdge *)source, (BMEdge *)target);
break;
case BM_LOOP:
bm_loop_attrs_copy(source_mesh, target_mesh, (const BMLoop *)source, (BMLoop *)target);
break;
case BM_FACE:
bm_face_attrs_copy(source_mesh, target_mesh, (const BMFace *)source, (BMFace *)target);
break;
default:
BLI_assert(0);
}
}
BMesh *BM_mesh_copy(BMesh *bmold)

@ -422,7 +422,7 @@ static float bmesh_loop_flip_equotion(float mat[2][2], float b[2], float target_
b[0] = coord[i];
b[1] = coord[j];
return mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0];
return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
}
static void bmesh_loop_flip_disp(float source_axis_x[3], float source_axis_y[3],
@ -449,8 +449,8 @@ static void bmesh_loop_flip_disp(float source_axis_x[3], float source_axis_y[3],
d = bmesh_loop_flip_equotion(mat, b, target_axis_x, target_axis_y, coord, 1, 2);
}
disp[0] = (b[0]*mat[1][1] - mat[0][1]*b[1]) / d;
disp[1] = (mat[0][0]*b[1] - b[0]*mat[1][0]) / d;
disp[0] = (b[0] * mat[1][1] - mat[0][1] * b[1]) / d;
disp[1] = (mat[0][0] * b[1] - b[0] * mat[1][0]) / d;
}
static void bmesh_loop_interp_mdisps(BMesh *bm, BMLoop *target, BMFace *source)

@ -479,9 +479,20 @@ void BM_elem_select_set(struct BMesh *bm, void *element, int select)
{
BMHeader *head = element;
if (head->htype == BM_VERT) BM_vert_select_set(bm, (BMVert *)element, select);
else if (head->htype == BM_EDGE) BM_edge_select_set(bm, (BMEdge *)element, select);
else if (head->htype == BM_FACE) BM_face_select_set(bm, (BMFace *)element, select);
switch (head->htype) {
case BM_VERT:
BM_vert_select_set(bm, (BMVert *)element, select);
break;
case BM_EDGE:
BM_edge_select_set(bm, (BMEdge *)element, select);
break;
case BM_FACE:
BM_face_select_set(bm, (BMFace *)element, select);
break;
default:
BLI_assert(0);
break;
}
}
/* this replaces the active flag used in uv/face mode */

@ -67,20 +67,22 @@ BMVert *BM_vert_create(BMesh *bm, const float co[3], const struct BMVert *exampl
v->head.htype = BM_VERT;
/* 'v->no' is handled by BM_elem_attrs_copy */
if (co) copy_v3_v3(v->co, co);
if (co) {
copy_v3_v3(v->co, co);
}
/* allocate flag */
v->oflags = BLI_mempool_calloc(bm->toolflagpool);
CustomData_bmesh_set_default(&bm->vdata, &v->head.data);
if (example) {
BM_elem_attrs_copy(bm, bm, (BMVert *)example, v);
BM_elem_attrs_copy(bm, bm, example, v);
}
BM_CHECK_ELEMENT(bm, v);
return (BMVert *) v;
return v;
}
/**
@ -112,7 +114,7 @@ BMEdge *BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *example,
BMEdge *e;
if (nodouble && (e = BM_edge_exists(v1, v2)))
return (BMEdge *)e;
return e;
e = BLI_mempool_calloc(bm->epool);
@ -131,8 +133,8 @@ BMEdge *BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *example,
/* allocate flag */
e->oflags = BLI_mempool_calloc(bm->toolflagpool);
e->v1 = (BMVert *) v1;
e->v2 = (BMVert *) v2;
e->v1 = v1;
e->v2 = v2;
BM_elem_flag_enable(e, BM_ELEM_SMOOTH);
@ -142,11 +144,11 @@ BMEdge *BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *example,
bmesh_disk_append_edge(e, e->v2);
if (example)
BM_elem_attrs_copy(bm, bm, (BMEdge *)example, (BMEdge *)e);
BM_elem_attrs_copy(bm, bm, example, e);
BM_CHECK_ELEMENT(bm, e);
return (BMEdge *) e;
return e;
}
static BMLoop *bmesh_create_loop(BMesh *bm, BMVert *v, BMEdge *e, BMFace *f, const BMLoop *example)
@ -292,14 +294,14 @@ BMFace *BM_face_create(BMesh *bm, BMVert **verts, BMEdge **edges, const int len,
f->head.htype = BM_FACE;
startl = lastl = bm_face_boundary_add(bm, (BMFace *)f, verts[0], edges[0]);
startl = lastl = bm_face_boundary_add(bm, f, verts[0], edges[0]);
startl->v = (BMVert *)verts[0];
startl->e = (BMEdge *)edges[0];
startl->v = verts[0];
startl->e = edges[0];
for (i = 1; i < len; i++) {
l = bmesh_create_loop(bm, verts[i], edges[i], (BMFace *)f, edges[i]->l);
l = bmesh_create_loop(bm, verts[i], edges[i], f, edges[i]->l);
l->f = (BMFace *) f;
l->f = f;
bmesh_radial_append(edges[i], l);
l->prev = lastl;
@ -323,7 +325,7 @@ BMFace *BM_face_create(BMesh *bm, BMVert **verts, BMEdge **edges, const int len,
BM_CHECK_ELEMENT(bm, f);
return (BMFace *) f;
return f;
}
int bmesh_check_element(BMesh *UNUSED(bm), void *element, const char htype)
@ -1083,7 +1085,7 @@ static BMFace *bmesh_addpolylist(BMesh *bm, BMFace *UNUSED(example))
f->totbounds = 1;
#endif
return (BMFace *) f;
return f;
}
/**

@ -31,34 +31,48 @@
#include "bmesh.h"
/* inserts a key/value mapping into a mapping slot. note that it copies the
* value, it doesn't store a reference to it. */
BM_INLINE void BMO_slot_map_insert(BMesh *UNUSED(bm), BMOperator *op, const char *slotname,
void *element, void *data, int len)
/* tool flag API. never, ever ever should tool code put junk in
* header flags (element->head.flag), nor should they use
* element->head.eflag1/eflag2. instead, use this api to set
* flags.
*
* if you need to store a value per element, use a
* ghash or a mapping slot to do it. */
/* flags 15 and 16 (1<<14 and 1<<15) are reserved for bmesh api use */
BM_INLINE int _bmo_elem_flag_test(BMesh *bm, BMFlagLayer *oflags, const short oflag)
{
BMOElemMapping *mapping;
BMOpSlot *slot = BMO_slot_get(op, slotname);
/*sanity check*/
if (slot->slottype != BMO_OP_SLOT_MAPPING) {
return;
}
mapping = (BMOElemMapping *) BLI_memarena_alloc(op->arena, sizeof(*mapping) + len);
mapping->element = (BMHeader*) element;
mapping->len = len;
memcpy(mapping + 1, data, len);
if (!slot->data.ghash) {
slot->data.ghash = BLI_ghash_new(BLI_ghashutil_ptrhash,
BLI_ghashutil_ptrcmp, "bmesh op");
}
BLI_ghash_insert(slot->data.ghash, element, mapping);
return oflags[bm->stackdepth-1].f & oflag;
}
BM_INLINE void _bmo_elem_flag_enable(BMesh *bm, BMFlagLayer *oflags, const short oflag)
{
oflags[bm->stackdepth-1].f |= oflag;
}
BM_INLINE void _bmo_elem_flag_disable(BMesh *bm, BMFlagLayer *oflags, const short oflag)
{
oflags[bm->stackdepth-1].f &= ~oflag;
}
BM_INLINE void _bmo_elem_flag_set(BMesh *bm, BMFlagLayer *oflags, const short oflag, int val)
{
if (val) oflags[bm->stackdepth-1].f |= oflag;
else oflags[bm->stackdepth-1].f &= ~oflag;
}
BM_INLINE void _bmo_elem_flag_toggle(BMesh *bm, BMFlagLayer *oflags, const short oflag)
{
oflags[bm->stackdepth-1].f ^= oflag;
}
#define BMO_elem_flag_test( bm, ele, oflag) _bmo_elem_flag_test (bm, (ele)->oflags, oflag)
#define BMO_elem_flag_enable( bm, ele, oflag) _bmo_elem_flag_enable (bm, (ele)->oflags, oflag)
#define BMO_elem_flag_disable(bm, ele, oflag) _bmo_elem_flag_disable (bm, (ele)->oflags, oflag)
#define BMO_elem_flag_set( bm, ele, oflag, val) _bmo_elem_flag_set (bm, (ele)->oflags, oflag, val)
#define BMO_elem_flag_toggle( bm, ele, oflag) _bmo_elem_flag_toggle (bm, (ele)->oflags, oflag)
BM_INLINE void BMO_slot_map_int_insert(BMesh *bm, BMOperator *op, const char *slotname,
void *element, int val)
{

@ -534,6 +534,34 @@ int BMO_slot_map_count(BMesh *UNUSED(bm), BMOperator *op, const char *slotname)
return slot->data.ghash ? BLI_ghash_size(slot->data.ghash) : 0;
}
/* inserts a key/value mapping into a mapping slot. note that it copies the
* value, it doesn't store a reference to it. */
void BMO_slot_map_insert(BMesh *UNUSED(bm), BMOperator *op, const char *slotname,
void *element, void *data, int len)
{
BMOElemMapping *mapping;
BMOpSlot *slot = BMO_slot_get(op, slotname);
/*sanity check*/
if (slot->slottype != BMO_OP_SLOT_MAPPING) {
return;
}
mapping = (BMOElemMapping *) BLI_memarena_alloc(op->arena, sizeof(*mapping) + len);
mapping->element = (BMHeader *) element;
mapping->len = len;
memcpy(mapping + 1, data, len);
if (!slot->data.ghash) {
slot->data.ghash = BLI_ghash_new(BLI_ghashutil_ptrhash,
BLI_ghashutil_ptrcmp, "bmesh op");
}
BLI_ghash_insert(slot->data.ghash, element, mapping);
}
#if 0
void *BMO_Grow_Array(BMesh *bm, BMOperator *op, int slotcode, int totadd)
{

@ -135,12 +135,12 @@ static void compute_poly_normal(float normal[3], float (*verts)[3], int nverts)
so thats?:
(a[1] - b[1]) * (a[2] + b[2]);
a[1]*b[2] - b[1]*a[2] - b[1]*b[2] + a[1]*a[2]
a[1] * b[2] - b[1] * a[2] - b[1] * b[2] + a[1] * a[2]
odd. half of that is the cross product. . .what's the
other half?
also could be like a[1]*(b[2] + a[2]) - b[1]*(a[2] - b[2])
also could be like a[1] * (b[2] + a[2]) - b[1] * (a[2] - b[2])
*/
n[0] += (u[1] - v[1]) * (u[2] + v[2]);

@ -43,15 +43,15 @@ struct BMLoop;
int bmesh_check_element(BMesh *bm, void *element, const char htype);
#define BM_CHECK_ELEMENT(bm, el) \
if (bmesh_check_element(bm, el, ((BMHeader*)el)->htype)) { \
if (bmesh_check_element(bm, el, ((BMHeader *)el)->htype)) { \
printf("check_element failure, with code %i on line %i in file\n" \
" \"%s\"\n\n", \
bmesh_check_element(bm, el, ((BMHeader*)el)->htype), \
bmesh_check_element(bm, el, ((BMHeader *)el)->htype), \
__LINE__, __FILE__); \
}
#define BM_EDGE_DISK_LINK_GET(e, v) ( \
((v) == ((BMEdge*)(e))->v1) ? \
((v) == ((BMEdge *)(e))->v1) ? \
&((e)->v1_disk_link) : \
&((e)->v2_disk_link) \
)

@ -191,7 +191,7 @@ void bmesh_disk_remove_edge(struct BMEdge *e, struct BMVert *v)
}
if (v->e == e)
v->e = (e != (BMEdge *)dl1->next) ? (BMEdge *)dl1->next : NULL;
v->e = (e != dl1->next) ? dl1->next : NULL;
dl1->next = dl1->prev = NULL;
}

@ -67,8 +67,7 @@ static void shellWalker_begin(BMWalker *walker, void *data)
BMEdge *e;
BMVert *v;
if (h == NULL)
{
if (UNLIKELY(h == NULL)) {
return;
}

@ -134,7 +134,7 @@ static void UNUSED_FUNCTION(rotsys_remove_edge)(struct BMEdge *e, struct BMVert
}
if (vd->e == e)
vd->e = (e != (BMEdge *)e1->next) ? (BMEdge *)e1->next : NULL;
vd->e = (e != e1->next) ? e1->next : NULL;
e1->next = e1->prev = NULL;
}

@ -518,7 +518,7 @@ void bmesh_finddoubles_common(BMesh *bm, BMOperator *op, BMOperator *optarget, c
/* Compare sort values of the verts using 3x tolerance (allowing for the tolerance
* on each of the three axes). This avoids the more expensive length comparison
* for most vertex pairs. */
if ((v2->co[0]+v2->co[1]+v2->co[2])-(v->co[0]+v->co[1]+v->co[2]) > dist3)
if ((v2->co[0] + v2->co[1] + v2->co[2]) - (v->co[0] + v->co[1] + v->co[2]) > dist3)
break;
if (keepvert) {

@ -459,7 +459,7 @@ static void quad_4edge_subdivide(BMesh *bm, BMFace *UNUSED(face), BMVert **verts
int numcuts = params->numcuts;
int i, j, a, b, s = numcuts + 2 /* , totv = numcuts * 4 + 4 */;
lines = MEM_callocN(sizeof(BMVert *)*(numcuts + 2)*(numcuts + 2), "q_4edge_split");
lines = MEM_callocN(sizeof(BMVert *) * (numcuts + 2) * (numcuts + 2), "q_4edge_split");
/* build a 2-dimensional array of verts,
* containing every vert (and all new ones)
* in the face */
@ -564,7 +564,7 @@ static void tri_3edge_subdivide(BMesh *bm, BMFace *UNUSED(face), BMVert **verts,
int i, j, a, b, numcuts = params->numcuts;
/* number of verts in each lin */
lines = MEM_callocN(sizeof(void *)*(numcuts + 2), "triangle vert table");
lines = MEM_callocN(sizeof(void *) * (numcuts + 2), "triangle vert table");
lines[0] = (BMVert **) stackarr;
lines[0][0] = verts[numcuts * 2 + 1];
@ -577,7 +577,7 @@ static void tri_3edge_subdivide(BMesh *bm, BMFace *UNUSED(face), BMVert **verts,
lines[numcuts + 1][numcuts + 1] = verts[numcuts];
for (i = 0; i < numcuts; i++) {
lines[i + 1] = MEM_callocN(sizeof(void *)*(2 + i), "triangle vert table row");
lines[i + 1] = MEM_callocN(sizeof(void *) * (2 + i), "triangle vert table row");
a = numcuts * 2 + 2 + i;
b = numcuts + numcuts - i;
e = connect_smallest_face(bm, verts[a], verts[b], &nf);

@ -271,7 +271,7 @@ void bmesh_righthandfaces_exec(BMesh *bm, BMOperator *op)
BMFace *f, *startf, **fstack = NULL;
BLI_array_declare(fstack);
BMLoop *l, *l2;
float maxx, cent[3];
float maxx, maxx_test, cent[3];
int i, maxi, flagflip = BMO_slot_bool_get(op, "do_flip");
startf = NULL;
@ -292,9 +292,8 @@ void bmesh_righthandfaces_exec(BMesh *bm, BMOperator *op)
BM_face_center_bounds_calc(bm, f, cent);
cent[0] = cent[0]*cent[0] + cent[1]*cent[1] + cent[2]*cent[2];
if (cent[0] > maxx) {
maxx = cent[0];
if ((maxx_test = dot_v3v3(cent, cent)) > maxx) {
maxx = maxx_test;
startf = f;
}
}

@ -1045,7 +1045,7 @@ static BMEdgeHit *knife_edge_tri_isect(knifetool_opdata *kcd, BMBVHTree *bmtree,
for (i = 0; i < tot; i++, result++) {
float p[3];
ls = (BMLoop**)kcd->em->looptris[result->indexA];
ls = (BMLoop **)kcd->em->looptris[result->indexA];
for (j = 0; j < 3; j++) {
BMLoop *l1 = ls[j];

@ -622,10 +622,10 @@ static void act_vert_def(Object *ob, BMVert **eve, MDeformVert **dvert)
if (ob && ob->mode & OB_MODE_EDIT && ob->type==OB_MESH && ob->defbase.first) {
Mesh *me= ob->data;
BMEditMesh *em = me->edit_btmesh;
BMEditSelection *ese= (BMEditSelection*)em->bm->selected.last;
BMEditSelection *ese = (BMEditSelection *)em->bm->selected.last;
if (ese && ese->htype == BM_VERT) {
*eve= (BMVert*)ese->data;
*eve= (BMVert *)ese->data;
*dvert= CustomData_bmesh_get(&em->bm->vdata, (*eve)->head.data, CD_MDEFORMVERT);
return;
}

@ -306,7 +306,7 @@ static DerivedMesh *arrayModifier_doArray(ArrayModifierData *amd,
BMO_op_callf(em->bm, "transform mat=%m4 verts=%s", offset, &op, "newout");
#define _E(s, i) ((BMVert**)(s)->data.buf)[i]
#define _E(s, i) ((BMVert **)(s)->data.buf)[i]
/*calculate merge mapping*/
if (j == 0) {