code cleanup: bmesh bvh
- remove unused members from BMBVHTree - move per-raycast vars from BMBVHTree into per-callback structs (weren't threadsafe). - simplify BKE_bmbvh_find_vert_closest().
This commit is contained in:
parent
b0752f1b89
commit
861a38ea09
|
@ -43,24 +43,18 @@
|
|||
|
||||
|
||||
struct BMBVHTree {
|
||||
BVHTree *tree;
|
||||
|
||||
BMEditMesh *em;
|
||||
BMesh *bm;
|
||||
BVHTree *tree;
|
||||
float epsilon;
|
||||
float maxdist; /* for nearest point search */
|
||||
float uv[2];
|
||||
|
||||
/* stuff for topological vert search */
|
||||
BMVert *v, *curv;
|
||||
GHash *gh;
|
||||
float curw, curd;
|
||||
float co[3], (*cagecos)[3], (*cos)[3];
|
||||
int curtag, flag;
|
||||
|
||||
float (*cos_cage)[3], (*cos)[3];
|
||||
int flag;
|
||||
};
|
||||
|
||||
struct CageUserData {
|
||||
int totvert;
|
||||
float (*cagecos)[3];
|
||||
float (*cos_cage)[3];
|
||||
BLI_bitmap vert_bitmap;
|
||||
};
|
||||
|
||||
|
@ -71,16 +65,19 @@ static void cage_mapped_verts_callback(void *userData, int index, const float co
|
|||
|
||||
if ((index >= 0 && index < data->totvert) && (!BLI_BITMAP_GET(data->vert_bitmap, index))) {
|
||||
BLI_BITMAP_SET(data->vert_bitmap, index);
|
||||
copy_v3_v3(data->cagecos[index], co);
|
||||
copy_v3_v3(data->cos_cage[index], co);
|
||||
}
|
||||
}
|
||||
|
||||
BMBVHTree *BKE_bmbvh_new(BMEditMesh *em, int flag, struct Scene *scene)
|
||||
{
|
||||
/* could become argument */
|
||||
const float epsilon = FLT_EPSILON * 2.0f;
|
||||
|
||||
struct BMLoop *(*looptris)[3] = em->looptris;
|
||||
BMBVHTree *bmtree = MEM_callocN(sizeof(*bmtree), "BMBVHTree");
|
||||
DerivedMesh *cage, *final;
|
||||
float cos[3][3], (*cagecos)[3] = NULL;
|
||||
float cos[3][3], (*cos_cage)[3] = NULL;
|
||||
int i;
|
||||
int tottri;
|
||||
|
||||
|
@ -92,7 +89,6 @@ BMBVHTree *BKE_bmbvh_new(BMEditMesh *em, int flag, struct Scene *scene)
|
|||
|
||||
bmtree->em = em;
|
||||
bmtree->bm = em->bm;
|
||||
bmtree->epsilon = FLT_EPSILON * 2.0f;
|
||||
bmtree->flag = flag;
|
||||
|
||||
if (flag & (BMBVH_RESPECT_SELECT)) {
|
||||
|
@ -115,7 +111,7 @@ BMBVHTree *BKE_bmbvh_new(BMEditMesh *em, int flag, struct Scene *scene)
|
|||
tottri = em->tottri;
|
||||
}
|
||||
|
||||
bmtree->tree = BLI_bvhtree_new(tottri, bmtree->epsilon, 8, 8);
|
||||
bmtree->tree = BLI_bvhtree_new(tottri, epsilon, 8, 8);
|
||||
|
||||
if (flag & BMBVH_USE_CAGE) {
|
||||
BLI_bitmap vert_bitmap;
|
||||
|
@ -132,14 +128,14 @@ BMBVHTree *BKE_bmbvh_new(BMEditMesh *em, int flag, struct Scene *scene)
|
|||
|
||||
|
||||
cage = editbmesh_get_derived_cage_and_final(scene, em->ob, em, &final, CD_MASK_DERIVEDMESH);
|
||||
cagecos = MEM_callocN(sizeof(float) * 3 * em->bm->totvert, "bmbvh cagecos");
|
||||
cos_cage = MEM_callocN(sizeof(float) * 3 * em->bm->totvert, "bmbvh cos_cage");
|
||||
|
||||
/* when initializing cage verts, we only want the first cage coordinate for each vertex,
|
||||
* so that e.g. mirror or array use original vertex coordinates and not mirrored or duplicate */
|
||||
vert_bitmap = BLI_BITMAP_NEW(em->bm->totvert, __func__);
|
||||
|
||||
data.totvert = em->bm->totvert;
|
||||
data.cagecos = cagecos;
|
||||
data.cos_cage = cos_cage;
|
||||
data.vert_bitmap = vert_bitmap;
|
||||
|
||||
cage->foreachMappedVert(cage, cage_mapped_verts_callback, &data);
|
||||
|
@ -147,7 +143,7 @@ BMBVHTree *BKE_bmbvh_new(BMEditMesh *em, int flag, struct Scene *scene)
|
|||
MEM_freeN(vert_bitmap);
|
||||
}
|
||||
|
||||
bmtree->cagecos = cagecos;
|
||||
bmtree->cos_cage = cos_cage;
|
||||
|
||||
for (i = 0; i < em->tottri; i++) {
|
||||
|
||||
|
@ -165,9 +161,9 @@ BMBVHTree *BKE_bmbvh_new(BMEditMesh *em, int flag, struct Scene *scene)
|
|||
}
|
||||
|
||||
if (flag & BMBVH_USE_CAGE) {
|
||||
copy_v3_v3(cos[0], cagecos[BM_elem_index_get(looptris[i][0]->v)]);
|
||||
copy_v3_v3(cos[1], cagecos[BM_elem_index_get(looptris[i][1]->v)]);
|
||||
copy_v3_v3(cos[2], cagecos[BM_elem_index_get(looptris[i][2]->v)]);
|
||||
copy_v3_v3(cos[0], cos_cage[BM_elem_index_get(looptris[i][0]->v)]);
|
||||
copy_v3_v3(cos[1], cos_cage[BM_elem_index_get(looptris[i][1]->v)]);
|
||||
copy_v3_v3(cos[2], cos_cage[BM_elem_index_get(looptris[i][2]->v)]);
|
||||
}
|
||||
else {
|
||||
copy_v3_v3(cos[0], looptris[i][0]->v->co);
|
||||
|
@ -187,19 +183,38 @@ void BKE_bmbvh_free(BMBVHTree *bmtree)
|
|||
{
|
||||
BLI_bvhtree_free(bmtree->tree);
|
||||
|
||||
if (bmtree->cagecos)
|
||||
MEM_freeN(bmtree->cagecos);
|
||||
if (bmtree->cos_cage)
|
||||
MEM_freeN(bmtree->cos_cage);
|
||||
if (bmtree->cos)
|
||||
MEM_freeN(bmtree->cos);
|
||||
|
||||
MEM_freeN(bmtree);
|
||||
}
|
||||
|
||||
BVHTree *BKE_bmbvh_tree_get(BMBVHTree *bmtree)
|
||||
{
|
||||
return bmtree->tree;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* Utility BMesh cast/intersect functions */
|
||||
|
||||
/* taken from bvhutils.c */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* BKE_bmbvh_ray_cast */
|
||||
|
||||
struct RayCastUserData {
|
||||
const BMLoop *(*looptris)[3];
|
||||
float uv[2];
|
||||
};
|
||||
|
||||
static void raycallback(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
|
||||
{
|
||||
BMBVHTree *bmtree = userdata;
|
||||
BMLoop **ltri = bmtree->em->looptris[index];
|
||||
struct RayCastUserData *bmcast_data = userdata;
|
||||
const BMLoop **ltri = bmcast_data->looptris[index];
|
||||
float dist, uv[2];
|
||||
const float *co1 = ltri[0]->v->co;
|
||||
const float *co2 = ltri[1]->v->co;
|
||||
|
@ -218,7 +233,7 @@ static void raycallback(void *userdata, int index, const BVHTreeRay *ray, BVHTre
|
|||
mul_v3_fl(hit->co, dist);
|
||||
add_v3_v3(hit->co, ray->origin);
|
||||
|
||||
copy_v2_v2(bmtree->uv, uv);
|
||||
copy_v2_v2(bmcast_data->uv, uv);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -226,19 +241,21 @@ BMFace *BKE_bmbvh_ray_cast(BMBVHTree *bmtree, const float co[3], const float dir
|
|||
float *r_dist, float r_hitout[3], float r_cagehit[3])
|
||||
{
|
||||
BVHTreeRayHit hit;
|
||||
struct RayCastUserData bmcast_data;
|
||||
const float dist = r_dist ? *r_dist : FLT_MAX;
|
||||
|
||||
hit.dist = dist;
|
||||
hit.index = -1;
|
||||
|
||||
zero_v2(bmtree->uv);
|
||||
/* ok to leave 'uv' uninitialized */
|
||||
bmcast_data.looptris = (const BMLoop *(*)[3])bmtree->em->looptris;
|
||||
|
||||
BLI_bvhtree_ray_cast(bmtree->tree, co, dir, 0.0f, &hit, raycallback, bmtree);
|
||||
BLI_bvhtree_ray_cast(bmtree->tree, co, dir, 0.0f, &hit, raycallback, &bmcast_data);
|
||||
if (hit.index != -1 && hit.dist != dist) {
|
||||
if (r_hitout) {
|
||||
if (bmtree->flag & BMBVH_RETURN_ORIG) {
|
||||
BMLoop **ltri = bmtree->em->looptris[hit.index];
|
||||
interp_v3_v3v3v3_uv(r_hitout, ltri[0]->v->co, ltri[1]->v->co, ltri[2]->v->co, bmtree->uv);
|
||||
interp_v3_v3v3v3_uv(r_hitout, ltri[0]->v->co, ltri[1]->v->co, ltri[2]->v->co, bmcast_data.uv);
|
||||
}
|
||||
else {
|
||||
copy_v3_v3(r_hitout, hit.co);
|
||||
|
@ -259,20 +276,24 @@ BMFace *BKE_bmbvh_ray_cast(BMBVHTree *bmtree, const float co[3], const float dir
|
|||
return NULL;
|
||||
}
|
||||
|
||||
BVHTree *BKE_bmbvh_tree_get(BMBVHTree *bmtree)
|
||||
{
|
||||
return bmtree->tree;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* BKE_bmbvh_find_vert_closest */
|
||||
|
||||
struct VertSearchUserData {
|
||||
const BMLoop *(*looptris)[3];
|
||||
float maxdist;
|
||||
int index_tri;
|
||||
};
|
||||
|
||||
static void vertsearchcallback(void *userdata, int index, const float *UNUSED(co), BVHTreeNearest *hit)
|
||||
{
|
||||
BMBVHTree *bmtree = userdata;
|
||||
BMLoop **ltri = bmtree->em->looptris[index];
|
||||
float dist, maxdist, v[3];
|
||||
struct VertSearchUserData *bmsearch_data = userdata;
|
||||
const BMLoop **ltri = bmsearch_data->looptris[index];
|
||||
const float maxdist = bmsearch_data->maxdist;
|
||||
float dist, v[3];
|
||||
int i;
|
||||
|
||||
maxdist = bmtree->maxdist;
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
sub_v3_v3v3(v, hit->co, ltri[i]->v->co);
|
||||
|
||||
|
@ -282,6 +303,7 @@ static void vertsearchcallback(void *userdata, int index, const float *UNUSED(co
|
|||
copy_v3_v3(hit->no, ltri[i]->v->no);
|
||||
hit->dist = dist;
|
||||
hit->index = index;
|
||||
bmsearch_data->index_tri = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -289,52 +311,21 @@ static void vertsearchcallback(void *userdata, int index, const float *UNUSED(co
|
|||
BMVert *BKE_bmbvh_find_vert_closest(BMBVHTree *bmtree, const float co[3], const float maxdist)
|
||||
{
|
||||
BVHTreeNearest hit;
|
||||
struct VertSearchUserData bmsearch_data;
|
||||
|
||||
copy_v3_v3(hit.co, co);
|
||||
/* XXX, why x5, scampbell */
|
||||
hit.dist = maxdist * 5;
|
||||
hit.index = -1;
|
||||
|
||||
bmtree->maxdist = maxdist;
|
||||
bmsearch_data.looptris = (const BMLoop *(*)[3])bmtree->em->looptris;
|
||||
bmsearch_data.maxdist = maxdist;
|
||||
|
||||
BLI_bvhtree_find_nearest(bmtree->tree, co, &hit, vertsearchcallback, bmtree);
|
||||
BLI_bvhtree_find_nearest(bmtree->tree, co, &hit, vertsearchcallback, &bmsearch_data);
|
||||
if (hit.dist != FLT_MAX && hit.index != -1) {
|
||||
BMLoop **ltri = bmtree->em->looptris[hit.index];
|
||||
float dist, curdist = bmtree->maxdist;
|
||||
int cur = 0, i;
|
||||
|
||||
/* maxdist = bmtree->maxdist; */ /* UNUSED */
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
dist = len_v3v3(hit.co, ltri[i]->v->co);
|
||||
if (dist < curdist) {
|
||||
cur = i;
|
||||
curdist = dist;
|
||||
}
|
||||
}
|
||||
|
||||
return ltri[cur]->v;
|
||||
return ltri[bmsearch_data.index_tri]->v;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* UNUSED */
|
||||
#if 0
|
||||
static short winding(const float v1[3], const float v2[3], const float v3[3])
|
||||
/* is v3 to the right of (v1 - v2) ? With exception: v3 == v1 || v3 == v2 */
|
||||
{
|
||||
double inp;
|
||||
|
||||
//inp = (v2[cox] - v1[cox]) * (v1[coy] - v3[coy]) + (v1[coy] - v2[coy]) * (v1[cox] - v3[cox]);
|
||||
inp = (v2[0] - v1[0]) * (v1[1] - v3[1]) + (v1[1] - v2[1]) * (v1[0] - v3[0]);
|
||||
|
||||
if (inp < 0.0) {
|
||||
return 0;
|
||||
}
|
||||
else if (inp == 0) {
|
||||
if (v1[0] == v3[0] && v1[1] == v3[1]) return 0;
|
||||
if (v2[0] == v3[0] && v2[1] == v3[1]) return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue