BKE bvhtree: Add `tree_type` parameter to `bvhtree_from_mesh_get`.
authorGermano <germano.costa@ig.com.br>
Thu, 3 May 2018 17:26:39 +0000 (14:26 -0300)
committerGermano <germano.costa@ig.com.br>
Thu, 3 May 2018 17:26:39 +0000 (14:26 -0300)
This will allow greater control of the bvhtrees that are obtained, and helps identify problems.
It is also an additional step to unify the functions.

13 files changed:
source/blender/blenkernel/BKE_bvhutils.h
source/blender/blenkernel/intern/bvhutils.c
source/blender/blenkernel/intern/constraint.c
source/blender/blenkernel/intern/mesh_remap.c
source/blender/blenkernel/intern/shrinkwrap.c
source/blender/blenlib/BLI_kdopbvh.h
source/blender/blenlib/intern/BLI_kdopbvh.c
source/blender/editors/physics/particle_object.c
source/blender/editors/transform/transform_snap_object.c
source/blender/modifiers/intern/MOD_surface.c
source/blender/modifiers/intern/MOD_surfacedeform.c
source/blender/modifiers/intern/MOD_weightvgproximity.c
source/blender/render/intern/source/bake_api.c

index 33e88a6b8f8fd066ac9b9bafe185c19bb1c7a4d9..ab3f9bc1a87edba573b1004dda1df23632d720f7 100644 (file)
@@ -161,7 +161,9 @@ 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);
+BVHTree *bvhtree_from_mesh_get(
+        struct BVHTreeFromMesh *data, struct DerivedMesh *mesh,
+        const int type, const int tree_type);
 
 /**
  * Frees data allocated by a call to bvhtree_from_mesh_*.
index 90a75b8d3cc53d77ee2c915091765842e7f13fb4..7f50fcfcb2b19f29a07cf6b65252a462d72bc3c2 100644 (file)
@@ -1229,19 +1229,30 @@ BVHTree *bvhtree_from_mesh_looptri_ex(
  * 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)
+        struct BVHTreeFromMesh *data, struct DerivedMesh *mesh,
+        const int type, const int tree_type)
 {
+       BVHTree *tree = NULL;
        switch (type) {
                case BVHTREE_FROM_VERTS:
-                       return bvhtree_from_mesh_verts(data, mesh, 0.0f, 2, 6);
+                       tree = bvhtree_from_mesh_verts(data, mesh, 0.0f, tree_type, 6);
+                       break;
                case BVHTREE_FROM_EDGES:
-                       return bvhtree_from_mesh_edges(data, mesh, 0.0f, 2, 6);
+                       tree = bvhtree_from_mesh_edges(data, mesh, 0.0f, tree_type, 6);
+                       break;
                case BVHTREE_FROM_FACES:
-                       return bvhtree_from_mesh_faces(data, mesh, 0.0f, 2, 6);
+                       tree = bvhtree_from_mesh_faces(data, mesh, 0.0f, tree_type, 6);
+                       break;
                case BVHTREE_FROM_LOOPTRI:
-                       return bvhtree_from_mesh_looptri(data, mesh, 0.0f, 2, 6);
+                       tree = bvhtree_from_mesh_looptri(data, mesh, 0.0f, tree_type, 6);
+                       break;
        }
-       return NULL;
+#ifdef DEBUG
+       if (BLI_bvhtree_get_tree_type(tree) != tree_type) {
+               printf("tree_type %d obtained instead of %d\n", BLI_bvhtree_get_tree_type(tree), tree_type);
+       }
+#endif
+       return tree;
 }
 
 /** \} */
index 934cfbe937490990058cad00507568dcf9aa35a3..4689de825b27c4a3608728362b1f09f1c4a36cc0 100644 (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_get(&treeData, target, BVHTREE_FROM_VERTS);
+                                               bvhtree_from_mesh_get(&treeData, target, BVHTREE_FROM_VERTS, 2);
                                        else
-                                               bvhtree_from_mesh_get(&treeData, target, BVHTREE_FROM_LOOPTRI);
+                                               bvhtree_from_mesh_get(&treeData, target, BVHTREE_FROM_LOOPTRI, 2);
                                        
                                        if (treeData.tree == NULL) {
                                                fail = true;
@@ -4178,7 +4178,7 @@ static void followtrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase
                                        sub_v3_v3v3(ray_nor, ray_end, ray_start);
                                        normalize_v3(ray_nor);
 
-                                       bvhtree_from_mesh_looptri(&treeData, target, 0.0f, 4, 6);
+                                       bvhtree_from_mesh_get(&treeData, target, BVHTREE_FROM_LOOPTRI, 4);
 
                                        hit.dist = BVH_RAYCAST_DIST_MAX;
                                        hit.index = -1;
index f99dd5574b21977d26be3053688d36bc20b1eb3b..c503799d28f4626910d071806c67f43f0291623b 100644 (file)
@@ -131,7 +131,7 @@ float BKE_mesh_remap_calc_difference_from_dm(
        float result = 0.0f;
        int i;
 
-       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_VERTS);
+       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_VERTS, 2);
        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_get(&treedata, dm_src, BVHTREE_FROM_VERTS);
+                       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_VERTS, 2);
                        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_get(&treedata, dm_src, BVHTREE_FROM_EDGES);
+                       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_EDGES, 2);
                        nearest.index = -1;
 
                        for (i = 0; i < numverts_dst; i++) {
@@ -548,7 +548,7 @@ void BKE_mesh_remap_calc_verts_from_dm(
                                        &treedata, dm_src, ray_radius, 2, 6);
                        }
                        else {
-                               bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_LOOPTRI);
+                               bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_LOOPTRI, 2);
                        }
 
                        if (mode == MREMAP_MODE_VERT_POLYINTERP_VNORPROJ) {
@@ -682,7 +682,7 @@ void BKE_mesh_remap_calc_edges_from_dm(
 
                        dm_src->getVertCos(dm_src, vcos_src);
 
-                       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_VERTS);
+                       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_VERTS, 2);
                        nearest.index = -1;
 
                        for (i = 0; i < numedges_dst; i++) {
@@ -782,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_get(&treedata, dm_src, BVHTREE_FROM_EDGES);
+                       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_EDGES, 2);
                        nearest.index = -1;
 
                        for (i = 0; i < numedges_dst; i++) {
@@ -809,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_get(&treedata, dm_src, BVHTREE_FROM_LOOPTRI);
+                       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_LOOPTRI, 2);
 
                        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);
@@ -1366,7 +1366,7 @@ void BKE_mesh_remap_calc_loops_from_dm(
                        }
                        else {
                                BLI_assert(num_trees == 1);
-                               bvhtree_from_mesh_get(&treedata[0], dm_src, BVHTREE_FROM_VERTS);
+                               bvhtree_from_mesh_get(&treedata[0], dm_src, BVHTREE_FROM_VERTS, 2);
                        }
                }
                else {  /* We use polygons. */
@@ -2018,7 +2018,7 @@ void BKE_mesh_remap_calc_polys_from_dm(
                                &treedata, dm_src, MREMAP_RAYCAST_APPROXIMATE_BVHEPSILON(ray_radius), 2, 6);
                }
                else {
-                       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_LOOPTRI);
+                       bvhtree_from_mesh_get(&treedata, dm_src, BVHTREE_FROM_LOOPTRI, 2);
                }
 
                if (mode == MREMAP_MODE_POLY_NEAREST) {
index 0d381248ef286be146d59cb05072f19a9f839d52..ef4ddc36d08d259721185e5f326ef88df5a6aff3 100644 (file)
@@ -158,7 +158,7 @@ static void shrinkwrap_calc_nearest_vertex(ShrinkwrapCalcData *calc)
                return;
        }
 
-       TIMEIT_BENCH(bvhtree_from_mesh_get(&treeData, calc->target, BVHTREE_FROM_VERTS), bvhtree_verts);
+       TIMEIT_BENCH(bvhtree_from_mesh_get(&treeData, calc->target, BVHTREE_FROM_VERTS, 2), bvhtree_verts);
        if (treeData.tree == NULL) {
                OUT_OF_MEMORY();
                return;
@@ -437,8 +437,8 @@ static void shrinkwrap_calc_normal_projection(ShrinkwrapCalcData *calc, bool for
                }
        }
        else {
-               if ((targ_tree = bvhtree_from_mesh_looptri(
-                       &treedata_stack.dmtreedata, calc->target, 0.0, 4, 6)))
+               if (targ_tree = bvhtree_from_mesh_get(
+                       &treedata_stack.dmtreedata, calc->target, BVHTREE_FROM_LOOPTRI, 4))
                {
                        targ_callback = treedata_stack.dmtreedata.raycast_callback;
                        treeData = &treedata_stack.dmtreedata;
@@ -459,7 +459,9 @@ static void shrinkwrap_calc_normal_projection(ShrinkwrapCalcData *calc, bool for
                                }
                        }
                        else {
-                               if ((aux_tree = bvhtree_from_mesh_looptri(&auxdata_stack.dmtreedata, auxMesh, 0.0, 4, 6)) != NULL) {
+                               if ((aux_tree = bvhtree_from_mesh_get(
+                                       &auxdata_stack.dmtreedata, auxMesh, BVHTREE_FROM_LOOPTRI, 4)) != NULL)
+                               {
                                        aux_callback = auxdata_stack.dmtreedata.raycast_callback;
                                        auxData = &auxdata_stack.dmtreedata;
                                }
@@ -588,7 +590,7 @@ static void shrinkwrap_calc_nearest_surface_point(ShrinkwrapCalcData *calc)
        }
 
        /* Create a bvh-tree of the given target */
-       bvhtree_from_mesh_get(&treeData, calc->target, BVHTREE_FROM_LOOPTRI);
+       bvhtree_from_mesh_get(&treeData, calc->target, BVHTREE_FROM_LOOPTRI, 2);
        if (treeData.tree == NULL) {
                OUT_OF_MEMORY();
                return;
index db53035dc7b9bac5b68b058fe9ffdaa909de4580..c92f40c67bf17a0072ee8535c0dcb3ea46e3698e 100644 (file)
@@ -130,7 +130,7 @@ BVHTreeOverlap *BLI_bvhtree_overlap(
         BVHTree_OverlapCallback callback, void *userdata);
 
 int   BLI_bvhtree_get_len(const BVHTree *tree);
-
+int   BLI_bvhtree_get_tree_type(const BVHTree *tree);
 float BLI_bvhtree_get_epsilon(const BVHTree *tree);
 
 /* find nearest node to the given coordinates
index 44ff78ea71a1a554f1336c370d234d53348248d9..027c6e084f5d0923a402220ac7d9e1e61e512b41 100644 (file)
@@ -1151,6 +1151,14 @@ int BLI_bvhtree_get_len(const BVHTree *tree)
        return tree->totleaf;
 }
 
+/**
+ * Maximum number of children that a node can have.
+ */
+int BLI_bvhtree_get_tree_type(const BVHTree *tree)
+{
+       return tree->tree_type;
+}
+
 float BLI_bvhtree_get_epsilon(const BVHTree *tree)
 {
        return tree->epsilon;
index f4334a2e87268358a7ecd9d81c9f381c12934208..1e204e9749934a31ebe59b6c910b2db9ae6257d8 100644 (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_get(&bvhtree, dm, BVHTREE_FROM_FACES);
+               bvhtree_from_mesh_get(&bvhtree, dm, BVHTREE_FROM_FACES, 2);
        }
        else if (dm->getNumEdges(dm) != 0) {
                medge = dm->getEdgeArray(dm);
-               bvhtree_from_mesh_get(&bvhtree, dm, BVHTREE_FROM_EDGES);
+               bvhtree_from_mesh_get(&bvhtree, dm, BVHTREE_FROM_EDGES, 2);
        }
        else {
                dm->release(dm);
index 569c662d31c5edac4f0d73091b4aea0e9f186a7a..94c473d6a658da80e51758c15b3d9b4125ea5284 100644 (file)
@@ -444,7 +444,7 @@ static bool raycastDerivedMesh(
                }
 
                if (treedata->tree == NULL) {
-                       bvhtree_from_mesh_looptri(treedata, dm, 0.0f, 4, 6);
+                       bvhtree_from_mesh_get(treedata, dm, BVHTREE_FROM_LOOPTRI, 4);
 
                        if (treedata->tree == NULL) {
                                return retval;
@@ -1687,10 +1687,10 @@ static bool snapDerivedMesh(
                if (treedata->tree == NULL) {
                        switch (snapdata->snap_to) {
                                case SCE_SNAP_MODE_EDGE:
-                                       bvhtree_from_mesh_get(treedata, dm, BVHTREE_FROM_EDGES);
+                                       bvhtree_from_mesh_get(treedata, dm, BVHTREE_FROM_EDGES, 2);
                                        break;
                                case SCE_SNAP_MODE_VERTEX:
-                                       bvhtree_from_mesh_get(treedata, dm, BVHTREE_FROM_VERTS);
+                                       bvhtree_from_mesh_get(treedata, dm, BVHTREE_FROM_VERTS, 2);
                                        break;
                        }
                }
index 389cffe85e7abd4e86b012a2e4e42cb58f57f4b7..bed8f95fb87b834ad29dc3a943ab96084614252c 100644 (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_get(surmd->bvhtree, surmd->dm, BVHTREE_FROM_LOOPTRI);
+                       bvhtree_from_mesh_get(surmd->bvhtree, surmd->dm, BVHTREE_FROM_LOOPTRI, 2);
                else
-                       bvhtree_from_mesh_get(surmd->bvhtree, surmd->dm, BVHTREE_FROM_EDGES);
+                       bvhtree_from_mesh_get(surmd->bvhtree, surmd->dm, BVHTREE_FROM_EDGES, 2);
        }
 }
 
index 5006fe4a6e006421d4128a092347480bb7686c0d..e0c94d456c2575c5c20de48baf41f7d44fa291ce 100644 (file)
@@ -960,7 +960,7 @@ static bool surfacedeformBind(SurfaceDeformModifierData *smd, float (*vertexCos)
                return false;
        }
 
-       bvhtree_from_mesh_get(&treeData, tdm, BVHTREE_FROM_LOOPTRI);
+       bvhtree_from_mesh_get(&treeData, tdm, BVHTREE_FROM_LOOPTRI, 2);
        if (treeData.tree == NULL) {
                modifier_setError((ModifierData *)smd, "Out of memory");
                freeAdjacencyMap(vert_edges, adj_array, edge_polys);
index c60c9690ac3d0bc638fef424cb5e44cbcb9506ac..2b17efe7b2446b6f1acf39cfbef0afbb37a0460d 100644 (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_get(&treeData_v, target, BVHTREE_FROM_VERTS);
+               bvhtree_from_mesh_get(&treeData_v, target, BVHTREE_FROM_VERTS, 2);
                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_get(&treeData_e, target, BVHTREE_FROM_EDGES);
+               bvhtree_from_mesh_get(&treeData_e, target, BVHTREE_FROM_EDGES, 2);
                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_get(&treeData_f, target, BVHTREE_FROM_LOOPTRI);
+               bvhtree_from_mesh_get(&treeData_f, target, BVHTREE_FROM_LOOPTRI, 2);
                if (treeData_f.tree == NULL) {
                        OUT_OF_MEMORY();
                        return;
index 7f7acceb5dde0da5d4d1a0113edb9b5426790404..8b25c1fe9626a6076c5e41f6c57cf85b6b3fad91 100644 (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_get(&treeData[i], dm_highpoly[i], BVHTREE_FROM_FACES);
+                       bvhtree_from_mesh_get(&treeData[i], dm_highpoly[i], BVHTREE_FROM_FACES, 2);
 
                        if (treeData[i].tree == NULL) {
                                printf("Baking: out of memory while creating BHVTree for object \"%s\"\n", highpoly[i].ob->id.name + 2);