Refactoring: bvhutils: Use a function that gets the bvhtree through an identifier type.

Reviewed By: @campbellbarton
Differential Revision: https://developer.blender.org/D3192
This commit is contained in:
Germano Cavalcante 2018-05-01 10:03:28 -03:00
parent 2e98524b58
commit 522bee3fc8
Notes: blender-bot 2023-02-14 11:25:11 +01:00
Referenced by issue #54947, Sculpt crashes Blender daily builds
11 changed files with 59 additions and 34 deletions

View File

@ -113,8 +113,6 @@ BVHTree *bvhtree_from_editmesh_verts_ex(
const BLI_bitmap *mask, int verts_num_active,
float epsilon, int tree_type, int axis);
BVHTree *bvhtree_from_mesh_verts(
struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis);
BVHTree *bvhtree_from_mesh_verts_ex(
struct BVHTreeFromMesh *data, const struct MVert *vert, const int numVerts,
const bool vert_allocated, const BLI_bitmap *mask, int verts_num_active,
@ -138,9 +136,6 @@ BVHTree *bvhtree_from_mesh_edges_ex(
const BLI_bitmap *edges_mask, int edges_num_active,
float epsilon, int tree_type, int axis);
BVHTree *bvhtree_from_mesh_faces(
struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon,
int tree_type, int axis);
BVHTree *bvhtree_from_mesh_faces_ex(
struct BVHTreeFromMesh *data,
const struct MVert *vert, const bool vert_allocated,
@ -166,6 +161,8 @@ BVHTree *bvhtree_from_mesh_looptri_ex(
const BLI_bitmap *mask, int looptri_num_active,
float epsilon, int tree_type, int axis);
BVHTree *bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, int type);
/**
* Frees data allocated by a call to bvhtree_from_mesh_*.
*/

View File

@ -501,7 +501,7 @@ BVHTree *bvhtree_from_editmesh_verts(
/* Builds a bvh tree where nodes are the vertices of the given dm
* and stores the BVHTree in dm->bvhCache */
BVHTree *bvhtree_from_mesh_verts(
static BVHTree *bvhtree_from_mesh_verts(
BVHTreeFromMesh *data, DerivedMesh *dm,
float epsilon, int tree_type, int axis)
{
@ -859,7 +859,7 @@ static void bvhtree_from_mesh_faces_setup_data(
}
/* Builds a bvh tree where nodes are the tesselated faces of the given dm */
BVHTree *bvhtree_from_mesh_faces(
static BVHTree *bvhtree_from_mesh_faces(
BVHTreeFromMesh *data, DerivedMesh *dm,
float epsilon, int tree_type, int axis)
{
@ -1225,6 +1225,25 @@ BVHTree *bvhtree_from_mesh_looptri_ex(
return tree;
}
/**
* Builds or queries a bvhcache for the cache bvhtree of the request type.
*/
BVHTree *bvhtree_from_mesh_get(
struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, int type)
{
switch (type) {
case BVHTREE_FROM_VERTS:
return bvhtree_from_mesh_verts(data, mesh, 0.0f, 2, 6);
case BVHTREE_FROM_EDGES:
return bvhtree_from_mesh_edges(data, mesh, 0.0f, 2, 6);
case BVHTREE_FROM_FACES:
return bvhtree_from_mesh_faces(data, mesh, 0.0f, 2, 6);
case BVHTREE_FROM_LOOPTRI:
return bvhtree_from_mesh_looptri(data, mesh, 0.0f, 2, 6);
}
return NULL;
}
/** \} */

View File

@ -3508,9 +3508,9 @@ static void shrinkwrap_get_tarmat(bConstraint *con, bConstraintOb *cob, bConstra
nearest.dist_sq = FLT_MAX;
if (scon->shrinkType == MOD_SHRINKWRAP_NEAREST_VERTEX)
bvhtree_from_mesh_verts(&treeData, target, 0.0, 2, 6);
bvhtree_from_mesh_get(&treeData, target, BVHTREE_FROM_VERTS);
else
bvhtree_from_mesh_looptri(&treeData, target, 0.0, 2, 6);
bvhtree_from_mesh_get(&treeData, target, BVHTREE_FROM_LOOPTRI);
if (treeData.tree == NULL) {
fail = true;

View File

@ -131,7 +131,7 @@ float BKE_mesh_remap_calc_difference_from_dm(
float result = 0.0f;
int i;
bvhtree_from_mesh_verts(&treedata, dm_src, 0.0f, 2, 6);
bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_VERTS);
nearest.index = -1;
for (i = 0; i < numverts_dst; i++) {
@ -460,7 +460,7 @@ void BKE_mesh_remap_calc_verts_from_dm(
float tmp_co[3], tmp_no[3];
if (mode == MREMAP_MODE_VERT_NEAREST) {
bvhtree_from_mesh_verts(&treedata, dm_src, 0.0f, 2, 6);
bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_VERTS);
nearest.index = -1;
for (i = 0; i < numverts_dst; i++) {
@ -485,7 +485,7 @@ void BKE_mesh_remap_calc_verts_from_dm(
float (*vcos_src)[3] = MEM_mallocN(sizeof(*vcos_src) * (size_t)dm_src->getNumVerts(dm_src), __func__);
dm_src->getVertCos(dm_src, vcos_src);
bvhtree_from_mesh_edges(&treedata, dm_src, 0.0f, 2, 6);
bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_EDGES);
nearest.index = -1;
for (i = 0; i < numverts_dst; i++) {
@ -543,7 +543,13 @@ void BKE_mesh_remap_calc_verts_from_dm(
float *weights = MEM_mallocN(sizeof(*weights) * tmp_buff_size, __func__);
dm_src->getVertCos(dm_src, vcos_src);
bvhtree_from_mesh_looptri(&treedata, dm_src, (mode & MREMAP_USE_NORPROJ) ? ray_radius : 0.0f, 2, 6);
if (mode & MREMAP_USE_NORPROJ) {
bvhtree_from_mesh_looptri(
&treedata, dm_src, ray_radius, 2, 6);
}
else {
bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_LOOPTRI);
}
if (mode == MREMAP_MODE_VERT_POLYINTERP_VNORPROJ) {
for (i = 0; i < numverts_dst; i++) {
@ -676,7 +682,7 @@ void BKE_mesh_remap_calc_edges_from_dm(
dm_src->getVertCos(dm_src, vcos_src);
bvhtree_from_mesh_verts(&treedata, dm_src, 0.0f, 2, 6);
bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_VERTS);
nearest.index = -1;
for (i = 0; i < numedges_dst; i++) {
@ -776,7 +782,7 @@ void BKE_mesh_remap_calc_edges_from_dm(
MEM_freeN(vert_to_edge_src_map_mem);
}
else if (mode == MREMAP_MODE_EDGE_NEAREST) {
bvhtree_from_mesh_edges(&treedata, dm_src, 0.0f, 2, 6);
bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_EDGES);
nearest.index = -1;
for (i = 0; i < numedges_dst; i++) {
@ -803,7 +809,7 @@ void BKE_mesh_remap_calc_edges_from_dm(
float (*vcos_src)[3] = MEM_mallocN(sizeof(*vcos_src) * (size_t)dm_src->getNumVerts(dm_src), __func__);
dm_src->getVertCos(dm_src, vcos_src);
bvhtree_from_mesh_looptri(&treedata, dm_src, 0.0f, 2, 6);
bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_LOOPTRI);
for (i = 0; i < numedges_dst; i++) {
interp_v3_v3v3(tmp_co, verts_dst[edges_dst[i].v1].co, verts_dst[edges_dst[i].v2].co, 0.5f);
@ -1360,7 +1366,7 @@ void BKE_mesh_remap_calc_loops_from_dm(
}
else {
BLI_assert(num_trees == 1);
bvhtree_from_mesh_verts(&treedata[0], dm_src, bvh_epsilon, 2, 6);
bvhtree_from_mesh_get(&treedata[0], dm_src, BVHTREE_FROM_VERTS);
}
}
else { /* We use polygons. */
@ -2007,10 +2013,13 @@ void BKE_mesh_remap_calc_polys_from_dm(
BVHTreeRayHit rayhit = {0};
float hit_dist;
bvhtree_from_mesh_looptri(
&treedata, dm_src,
(mode & MREMAP_USE_NORPROJ) ? MREMAP_RAYCAST_APPROXIMATE_BVHEPSILON(ray_radius) : 0.0f,
2, 6);
if (mode & MREMAP_USE_NORPROJ) {
bvhtree_from_mesh_looptri(
&treedata, dm_src, MREMAP_RAYCAST_APPROXIMATE_BVHEPSILON(ray_radius), 2, 6);
}
else {
bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_LOOPTRI);
}
if (mode == MREMAP_MODE_POLY_NEAREST) {
nearest.index = -1;

View File

@ -158,7 +158,7 @@ static void shrinkwrap_calc_nearest_vertex(ShrinkwrapCalcData *calc)
return;
}
TIMEIT_BENCH(bvhtree_from_mesh_verts(&treeData, calc->target, 0.0, 2, 6), bvhtree_verts);
TIMEIT_BENCH(bvhtree_from_mesh_get(&treeData, calc->target, BVHTREE_FROM_VERTS), bvhtree_verts);
if (treeData.tree == NULL) {
OUT_OF_MEMORY();
return;
@ -588,7 +588,7 @@ static void shrinkwrap_calc_nearest_surface_point(ShrinkwrapCalcData *calc)
}
/* Create a bvh-tree of the given target */
bvhtree_from_mesh_looptri(&treeData, calc->target, 0.0, 2, 6);
bvhtree_from_mesh_get(&treeData, calc->target, BVHTREE_FROM_LOOPTRI);
if (treeData.tree == NULL) {
OUT_OF_MEMORY();
return;

View File

@ -692,11 +692,11 @@ static bool remap_hair_emitter(Scene *scene, Object *ob, ParticleSystem *psys,
if (dm->getNumTessFaces(dm) != 0) {
mface = dm->getTessFaceArray(dm);
bvhtree_from_mesh_faces(&bvhtree, dm, 0.0, 2, 6);
bvhtree_from_mesh_get(&bvhtree, dm, BVHTREE_FROM_FACES);
}
else if (dm->getNumEdges(dm) != 0) {
medge = dm->getEdgeArray(dm);
bvhtree_from_mesh_edges(&bvhtree, dm, 0.0, 2, 6);
bvhtree_from_mesh_get(&bvhtree, dm, BVHTREE_FROM_EDGES);
}
else {
dm->release(dm);

View File

@ -1687,10 +1687,10 @@ static bool snapDerivedMesh(
if (treedata->tree == NULL) {
switch (snapdata->snap_to) {
case SCE_SNAP_MODE_EDGE:
bvhtree_from_mesh_edges(treedata, dm, 0.0f, 2, 6);
bvhtree_from_mesh_get(treedata, dm, BVHTREE_FROM_EDGES);
break;
case SCE_SNAP_MODE_VERTEX:
bvhtree_from_mesh_verts(treedata, dm, 0.0f, 2, 6);
bvhtree_from_mesh_get(treedata, dm, BVHTREE_FROM_VERTS);
break;
}
}

View File

@ -159,9 +159,9 @@ static void deformVerts(ModifierData *md, Object *ob,
surmd->bvhtree = MEM_callocN(sizeof(BVHTreeFromMesh), "BVHTreeFromMesh");
if (surmd->dm->getNumPolys(surmd->dm))
bvhtree_from_mesh_looptri(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
bvhtree_from_mesh_get(surmd->bvhtree, surmd->dm, BVHTREE_FROM_LOOPTRI);
else
bvhtree_from_mesh_edges(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
bvhtree_from_mesh_get(surmd->bvhtree, surmd->dm, BVHTREE_FROM_EDGES);
}
}

View File

@ -960,7 +960,7 @@ static bool surfacedeformBind(SurfaceDeformModifierData *smd, float (*vertexCos)
return false;
}
bvhtree_from_mesh_looptri(&treeData, tdm, 0.0, 2, 6);
bvhtree_from_mesh_get(&treeData, tdm, BVHTREE_FROM_LOOPTRI);
if (treeData.tree == NULL) {
modifier_setError((ModifierData *)smd, "Out of memory");
freeAdjacencyMap(vert_edges, adj_array, edge_polys);

View File

@ -149,7 +149,7 @@ static void get_vert2geom_distance(int numVerts, float (*v_cos)[3],
if (dist_v) {
/* Create a bvh-tree of the given target's verts. */
bvhtree_from_mesh_verts(&treeData_v, target, 0.0, 2, 6);
bvhtree_from_mesh_get(&treeData_v, target, BVHTREE_FROM_VERTS);
if (treeData_v.tree == NULL) {
OUT_OF_MEMORY();
return;
@ -157,7 +157,7 @@ static void get_vert2geom_distance(int numVerts, float (*v_cos)[3],
}
if (dist_e) {
/* Create a bvh-tree of the given target's edges. */
bvhtree_from_mesh_edges(&treeData_e, target, 0.0, 2, 6);
bvhtree_from_mesh_get(&treeData_e, target, BVHTREE_FROM_EDGES);
if (treeData_e.tree == NULL) {
OUT_OF_MEMORY();
return;
@ -165,7 +165,7 @@ static void get_vert2geom_distance(int numVerts, float (*v_cos)[3],
}
if (dist_f) {
/* Create a bvh-tree of the given target's faces. */
bvhtree_from_mesh_looptri(&treeData_f, target, 0.0, 2, 6);
bvhtree_from_mesh_get(&treeData_f, target, BVHTREE_FROM_LOOPTRI);
if (treeData_f.tree == NULL) {
OUT_OF_MEMORY();
return;

View File

@ -526,7 +526,7 @@ bool RE_bake_pixels_populate_from_objects(
if (dm_highpoly[i]->getNumTessFaces(dm_highpoly[i]) != 0) {
/* Create a bvh-tree for each highpoly object */
bvhtree_from_mesh_faces(&treeData[i], dm_highpoly[i], 0.0, 2, 6);
bvhtree_from_mesh_get(&treeData[i], dm_highpoly[i], BVHTREE_FROM_FACES);
if (treeData[i].tree == NULL) {
printf("Baking: out of memory while creating BHVTree for object \"%s\"\n", highpoly[i].ob->id.name + 2);