diff options
author | Germano Cavalcante <germano.costa@ig.com.br> | 2022-04-04 23:41:36 +0300 |
---|---|---|
committer | Germano Cavalcante <germano.costa@ig.com.br> | 2022-04-06 00:14:22 +0300 |
commit | 228f7f1c850897cac85b2c4b42cf9052976b7be1 (patch) | |
tree | e36173f38cd1ea97e698805fdbbd192c53334234 /source/blender/blenkernel | |
parent | 7a5e663843786f7a2d051e06de9586cca7ed5c39 (diff) |
Refactor: Deduplicate and simplify BVH Utils code
In summary the changes are:
- Merge all `bvhtree_from_mesh_*.*_setup_data` in a single utility
- Create `bvhtree_from_editmesh_setup_data`
- Setup data only once in `BKE_bvhtree_from_mesh_get` and `BKE_bvhtree_from_editmesh_get`
Also the behavior of `BKE_bvhtree_from_mesh_get` and
`BKE_bvhtree_from_editmesh_get` changed a bit:
- If a null tree is cached, don't set the `data` to zero. This tree is not an error and the others data can still be used.
- If a null tree is returned, don't set the `data` to zero. Matches the previous change.
Differential Revision: https://developer.blender.org/D14549
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/intern/bvhutils.cc | 638 |
1 files changed, 299 insertions, 339 deletions
diff --git a/source/blender/blenkernel/intern/bvhutils.cc b/source/blender/blenkernel/intern/bvhutils.cc index 01e4eb702cc..1cab5281f0e 100644 --- a/source/blender/blenkernel/intern/bvhutils.cc +++ b/source/blender/blenkernel/intern/bvhutils.cc @@ -562,6 +562,103 @@ static void mesh_edges_spherecast(void *userdata, */ /* -------------------------------------------------------------------- */ +/** \name Commom Utils + * \{ */ + +static void bvhtree_from_mesh_setup_data(BVHTree *tree, + const BVHCacheType bvh_cache_type, + const MVert *vert, + const MEdge *edge, + const MFace *face, + const MLoop *loop, + const MLoopTri *looptri, + const float (*vert_normals)[3], + BVHTreeFromMesh *r_data) +{ + memset(r_data, 0, sizeof(*r_data)); + + r_data->tree = tree; + + r_data->vert = vert; + r_data->edge = edge; + r_data->face = face; + r_data->loop = loop; + r_data->looptri = looptri; + r_data->vert_normals = vert_normals; + + switch (bvh_cache_type) { + case BVHTREE_FROM_VERTS: + case BVHTREE_FROM_LOOSEVERTS: + /* a nullptr nearest callback works fine + * remember the min distance to point is the same as the min distance to BV of point */ + r_data->nearest_callback = nullptr; + r_data->raycast_callback = mesh_verts_spherecast; + break; + + case BVHTREE_FROM_EDGES: + case BVHTREE_FROM_LOOSEEDGES: + r_data->nearest_callback = mesh_edges_nearest_point; + r_data->raycast_callback = mesh_edges_spherecast; + break; + case BVHTREE_FROM_FACES: + r_data->nearest_callback = mesh_faces_nearest_point; + r_data->raycast_callback = mesh_faces_spherecast; + break; + case BVHTREE_FROM_LOOPTRI: + case BVHTREE_FROM_LOOPTRI_NO_HIDDEN: + r_data->nearest_callback = mesh_looptri_nearest_point; + r_data->raycast_callback = mesh_looptri_spherecast; + break; + case BVHTREE_FROM_EM_VERTS: + case BVHTREE_FROM_EM_EDGES: + case BVHTREE_FROM_EM_LOOPTRI: + case BVHTREE_MAX_ITEM: + BLI_assert(false); + break; + } +} + +static void bvhtree_from_editmesh_setup_data(BVHTree *tree, + const BVHCacheType bvh_cache_type, + struct BMEditMesh *em, + BVHTreeFromEditMesh *r_data) +{ + memset(r_data, 0, sizeof(*r_data)); + + r_data->tree = tree; + + r_data->em = em; + + switch (bvh_cache_type) { + case BVHTREE_FROM_EM_VERTS: + r_data->nearest_callback = nullptr; + r_data->raycast_callback = editmesh_verts_spherecast; + break; + case BVHTREE_FROM_EM_EDGES: + r_data->nearest_callback = nullptr; /* TODO */ + r_data->raycast_callback = nullptr; /* TODO */ + break; + case BVHTREE_FROM_EM_LOOPTRI: + r_data->nearest_callback = editmesh_looptri_nearest_point; + r_data->raycast_callback = editmesh_looptri_spherecast; + break; + + case BVHTREE_FROM_VERTS: + case BVHTREE_FROM_LOOSEVERTS: + case BVHTREE_FROM_EDGES: + case BVHTREE_FROM_LOOSEEDGES: + case BVHTREE_FROM_FACES: + case BVHTREE_FROM_LOOPTRI: + case BVHTREE_FROM_LOOPTRI_NO_HIDDEN: + case BVHTREE_MAX_ITEM: + BLI_assert(false); + break; + } +} + +/** \} */ + +/* -------------------------------------------------------------------- */ /** \name Vertex Builder * \{ */ @@ -631,26 +728,6 @@ static BVHTree *bvhtree_from_mesh_verts_create_tree(float epsilon, return tree; } -static void bvhtree_from_mesh_verts_setup_data(BVHTreeFromMesh *data, - BVHTree *tree, - const bool is_cached, - const MVert *vert, - const bool vert_allocated) -{ - memset(data, 0, sizeof(*data)); - - data->tree = tree; - data->cached = is_cached; - - /* a nullptr nearest callback works fine - * remember the min distance to point is the same as the min distance to BV of point */ - data->nearest_callback = nullptr; - data->raycast_callback = mesh_verts_spherecast; - - data->vert = vert; - data->vert_allocated = vert_allocated; -} - BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data, BMEditMesh *em, const BLI_bitmap *verts_mask, @@ -687,12 +764,8 @@ BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data, bvhtree_balance(tree, false); } - if (tree) { - memset(data, 0, sizeof(*data)); - data->tree = tree; - data->em = em; - data->nearest_callback = nullptr; - data->raycast_callback = editmesh_verts_spherecast; + if (data) { + bvhtree_from_editmesh_setup_data(tree, BVHTREE_FROM_EM_VERTS, em, data); data->cached = bvh_cache_p != nullptr; } @@ -744,8 +817,13 @@ BVHTree *bvhtree_from_mesh_verts_ex(BVHTreeFromMesh *data, bvhcache_unlock(*bvh_cache_p, lock_started); } - /* Setup BVHTreeFromMesh */ - bvhtree_from_mesh_verts_setup_data(data, tree, in_cache, vert, vert_allocated); + if (data) { + /* Setup BVHTreeFromMesh */ + bvhtree_from_mesh_setup_data( + tree, BVHTREE_FROM_VERTS, vert, nullptr, nullptr, nullptr, nullptr, nullptr, data); + data->vert_allocated = vert_allocated; + data->cached = in_cache; + } return tree; } @@ -833,29 +911,6 @@ static BVHTree *bvhtree_from_mesh_edges_create_tree(const MVert *vert, return tree; } -static void bvhtree_from_mesh_edges_setup_data(BVHTreeFromMesh *data, - BVHTree *tree, - const bool is_cached, - const MVert *vert, - const bool vert_allocated, - const MEdge *edge, - const bool edge_allocated) -{ - memset(data, 0, sizeof(*data)); - - data->tree = tree; - - data->cached = is_cached; - - data->nearest_callback = mesh_edges_nearest_point; - data->raycast_callback = mesh_edges_spherecast; - - data->vert = vert; - data->vert_allocated = vert_allocated; - data->edge = edge; - data->edge_allocated = edge_allocated; -} - BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data, BMEditMesh *em, const BLI_bitmap *edges_mask, @@ -891,12 +946,8 @@ BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data, bvhtree_balance(tree, false); } - if (tree) { - memset(data, 0, sizeof(*data)); - data->tree = tree; - data->em = em; - data->nearest_callback = nullptr; /* TODO */ - data->raycast_callback = nullptr; /* TODO */ + if (data) { + bvhtree_from_editmesh_setup_data(tree, BVHTREE_FROM_EM_EDGES, em, data); data->cached = bvh_cache_p != nullptr; } @@ -954,9 +1005,14 @@ BVHTree *bvhtree_from_mesh_edges_ex(BVHTreeFromMesh *data, bvhcache_unlock(*bvh_cache_p, lock_started); } - /* Setup BVHTreeFromMesh */ - bvhtree_from_mesh_edges_setup_data( - data, tree, in_cache, vert, vert_allocated, edge, edge_allocated); + if (data) { + /* Setup BVHTreeFromMesh */ + bvhtree_from_mesh_setup_data( + tree, BVHTREE_FROM_EDGES, vert, edge, nullptr, nullptr, nullptr, nullptr, data); + data->vert_allocated = vert_allocated; + data->vert_allocated = edge_allocated; + data->cached = in_cache; + } return tree; } @@ -1014,28 +1070,6 @@ static BVHTree *bvhtree_from_mesh_faces_create_tree(float epsilon, return tree; } -static void bvhtree_from_mesh_faces_setup_data(BVHTreeFromMesh *data, - BVHTree *tree, - const bool is_cached, - const MVert *vert, - const bool vert_allocated, - const MFace *face, - const bool face_allocated) -{ - memset(data, 0, sizeof(*data)); - - data->tree = tree; - data->cached = is_cached; - - data->nearest_callback = mesh_faces_nearest_point; - data->raycast_callback = mesh_faces_spherecast; - - data->vert = vert; - data->vert_allocated = vert_allocated; - data->face = face; - data->face_allocated = face_allocated; -} - BVHTree *bvhtree_from_mesh_faces_ex(BVHTreeFromMesh *data, const MVert *vert, const bool vert_allocated, @@ -1076,9 +1110,14 @@ BVHTree *bvhtree_from_mesh_faces_ex(BVHTreeFromMesh *data, bvhcache_unlock(*bvh_cache_p, lock_started); } - /* Setup BVHTreeFromMesh */ - bvhtree_from_mesh_faces_setup_data( - data, tree, in_cache, vert, vert_allocated, face, face_allocated); + if (data) { + /* Setup BVHTreeFromMesh */ + bvhtree_from_mesh_setup_data( + tree, BVHTREE_FROM_FACES, vert, nullptr, face, nullptr, nullptr, nullptr, data); + data->vert_allocated = vert_allocated; + data->face_allocated = face_allocated; + data->cached = in_cache; + } return tree; } @@ -1183,32 +1222,6 @@ static BVHTree *bvhtree_from_mesh_looptri_create_tree(float epsilon, return tree; } -static void bvhtree_from_mesh_looptri_setup_data(BVHTreeFromMesh *data, - BVHTree *tree, - const bool is_cached, - const MVert *vert, - const bool vert_allocated, - const MLoop *mloop, - const bool loop_allocated, - const MLoopTri *looptri, - const bool looptri_allocated) -{ - memset(data, 0, sizeof(*data)); - - data->tree = tree; - data->cached = is_cached; - - data->nearest_callback = mesh_looptri_nearest_point; - data->raycast_callback = mesh_looptri_spherecast; - - data->vert = vert; - data->vert_allocated = vert_allocated; - data->loop = mloop; - data->loop_allocated = loop_allocated; - data->looptri = looptri; - data->looptri_allocated = looptri_allocated; -} - BVHTree *bvhtree_from_editmesh_looptri_ex(BVHTreeFromEditMesh *data, BMEditMesh *em, const BLI_bitmap *looptri_mask, @@ -1246,11 +1259,8 @@ BVHTree *bvhtree_from_editmesh_looptri_ex(BVHTreeFromEditMesh *data, bvhtree_balance(tree, false); } - if (tree) { - data->tree = tree; - data->nearest_callback = editmesh_looptri_nearest_point; - data->raycast_callback = editmesh_looptri_spherecast; - data->em = em; + if (data) { + bvhtree_from_editmesh_setup_data(tree, BVHTREE_FROM_EM_LOOPTRI, em, data); data->cached = bvh_cache_p != nullptr; } return tree; @@ -1312,16 +1322,15 @@ BVHTree *bvhtree_from_mesh_looptri_ex(BVHTreeFromMesh *data, bvhcache_unlock(*bvh_cache_p, lock_started); } - /* Setup BVHTreeFromMesh */ - bvhtree_from_mesh_looptri_setup_data(data, - tree, - in_cache, - vert, - vert_allocated, - mloop, - loop_allocated, - looptri, - looptri_allocated); + if (data) { + /* Setup BVHTreeFromMesh */ + bvhtree_from_mesh_setup_data( + tree, BVHTREE_FROM_LOOPTRI, vert, nullptr, nullptr, mloop, looptri, nullptr, data); + data->vert_allocated = vert_allocated; + data->loop_allocated = loop_allocated; + data->looptri_allocated = looptri_allocated; + data->cached = in_cache; + } return tree; } @@ -1410,158 +1419,143 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, const BVHCacheType bvh_cache_type, const int tree_type) { - BVHTree *tree = nullptr; BVHCache **bvh_cache_p = (BVHCache **)&mesh->runtime.bvh_cache; ThreadMutex *mesh_eval_mutex = (ThreadMutex *)mesh->runtime.eval_mutex; - const bool is_cached = bvhcache_find(bvh_cache_p, bvh_cache_type, &tree, nullptr, nullptr); - - if (is_cached && tree == nullptr) { - memset(data, 0, sizeof(*data)); - return tree; + /* Setup BVHTreeFromMesh */ + bvhtree_from_mesh_setup_data( + nullptr, + bvh_cache_type, + mesh->mvert, + mesh->medge, + mesh->mface, + mesh->mloop, + ELEM(bvh_cache_type, BVHTREE_FROM_LOOPTRI, BVHTREE_FROM_LOOPTRI_NO_HIDDEN) ? + BKE_mesh_runtime_looptri_ensure(mesh) : + nullptr, + BKE_mesh_vertex_normals_ensure(mesh), + data); + + data->cached = bvhcache_find(bvh_cache_p, bvh_cache_type, &data->tree, nullptr, nullptr); + + if (data->cached) { + /* NOTE: #data->tree can be nullptr. */ + return data->tree; } switch (bvh_cache_type) { case BVHTREE_FROM_VERTS: - case BVHTREE_FROM_LOOSEVERTS: - if (is_cached == false) { - BLI_bitmap *loose_verts_mask = nullptr; - int loose_vert_len = -1; - int verts_len = mesh->totvert; - - if (bvh_cache_type == BVHTREE_FROM_LOOSEVERTS) { - loose_verts_mask = loose_verts_map_get( - mesh->medge, mesh->totedge, mesh->mvert, verts_len, &loose_vert_len); - } - - tree = bvhtree_from_mesh_verts_ex(data, - mesh->mvert, - verts_len, - false, - loose_verts_mask, - loose_vert_len, - 0.0f, - tree_type, - 6, - bvh_cache_type, - bvh_cache_p, - mesh_eval_mutex); - - if (loose_verts_mask != nullptr) { - MEM_freeN(loose_verts_mask); - } + case BVHTREE_FROM_LOOSEVERTS: { + BLI_bitmap *loose_verts_mask = nullptr; + int loose_vert_len = -1; + int verts_len = mesh->totvert; + + if (bvh_cache_type == BVHTREE_FROM_LOOSEVERTS) { + loose_verts_mask = loose_verts_map_get( + mesh->medge, mesh->totedge, mesh->mvert, verts_len, &loose_vert_len); } - else { - /* Setup BVHTreeFromMesh */ - bvhtree_from_mesh_verts_setup_data(data, tree, true, mesh->mvert, false); + + data->tree = bvhtree_from_mesh_verts_ex(nullptr, + mesh->mvert, + verts_len, + false, + loose_verts_mask, + loose_vert_len, + 0.0f, + tree_type, + 6, + bvh_cache_type, + bvh_cache_p, + mesh_eval_mutex); + + if (loose_verts_mask != nullptr) { + MEM_freeN(loose_verts_mask); } - break; + } break; case BVHTREE_FROM_EDGES: - case BVHTREE_FROM_LOOSEEDGES: - if (is_cached == false) { - BLI_bitmap *loose_edges_mask = nullptr; - int loose_edges_len = -1; - int edges_len = mesh->totedge; + case BVHTREE_FROM_LOOSEEDGES: { + BLI_bitmap *loose_edges_mask = nullptr; + int loose_edges_len = -1; + int edges_len = mesh->totedge; - if (bvh_cache_type == BVHTREE_FROM_LOOSEEDGES) { - loose_edges_mask = loose_edges_map_get(mesh->medge, edges_len, &loose_edges_len); - } - - tree = bvhtree_from_mesh_edges_ex(data, - mesh->mvert, - false, - mesh->medge, - edges_len, - false, - loose_edges_mask, - loose_edges_len, - 0.0, - tree_type, - 6, - bvh_cache_type, - bvh_cache_p, - mesh_eval_mutex); - - if (loose_edges_mask != nullptr) { - MEM_freeN(loose_edges_mask); - } - } - else { - /* Setup BVHTreeFromMesh */ - bvhtree_from_mesh_edges_setup_data( - data, tree, true, mesh->mvert, false, mesh->medge, false); + if (bvh_cache_type == BVHTREE_FROM_LOOSEEDGES) { + loose_edges_mask = loose_edges_map_get(mesh->medge, edges_len, &loose_edges_len); } - break; - case BVHTREE_FROM_FACES: - if (is_cached == false) { - int num_faces = mesh->totface; - BLI_assert(!(num_faces == 0 && mesh->totpoly != 0)); - - tree = bvhtree_from_mesh_faces_ex(data, - mesh->mvert, - false, - mesh->mface, - num_faces, - false, - nullptr, - -1, - 0.0, - tree_type, - 6, - bvh_cache_type, - bvh_cache_p, - mesh_eval_mutex); - } - else { - /* Setup BVHTreeFromMesh */ - bvhtree_from_mesh_faces_setup_data( - data, tree, true, mesh->mvert, false, mesh->mface, false); + data->tree = bvhtree_from_mesh_edges_ex(nullptr, + mesh->mvert, + false, + mesh->medge, + edges_len, + false, + loose_edges_mask, + loose_edges_len, + 0.0, + tree_type, + 6, + bvh_cache_type, + bvh_cache_p, + mesh_eval_mutex); + + if (loose_edges_mask != nullptr) { + MEM_freeN(loose_edges_mask); } - break; + } break; + + case BVHTREE_FROM_FACES: { + int num_faces = mesh->totface; + BLI_assert(!(num_faces == 0 && mesh->totpoly != 0)); + + data->tree = bvhtree_from_mesh_faces_ex(nullptr, + mesh->mvert, + false, + mesh->mface, + num_faces, + false, + nullptr, + -1, + 0.0, + tree_type, + 6, + bvh_cache_type, + bvh_cache_p, + mesh_eval_mutex); + } break; case BVHTREE_FROM_LOOPTRI: - case BVHTREE_FROM_LOOPTRI_NO_HIDDEN: - if (is_cached == false) { - const MLoopTri *mlooptri = BKE_mesh_runtime_looptri_ensure(mesh); - int looptri_len = BKE_mesh_runtime_looptri_len(mesh); - - int looptri_mask_active_len = -1; - BLI_bitmap *looptri_mask = nullptr; - if (bvh_cache_type == BVHTREE_FROM_LOOPTRI_NO_HIDDEN) { - looptri_mask = looptri_no_hidden_map_get( - mesh->mpoly, looptri_len, &looptri_mask_active_len); - } - - tree = bvhtree_from_mesh_looptri_ex(data, - mesh->mvert, - false, - mesh->mloop, - false, - mlooptri, - looptri_len, - false, - looptri_mask, - looptri_mask_active_len, - 0.0, - tree_type, - 6, - bvh_cache_type, - bvh_cache_p, - mesh_eval_mutex); - - if (looptri_mask != nullptr) { - MEM_freeN(looptri_mask); - } + case BVHTREE_FROM_LOOPTRI_NO_HIDDEN: { + const MLoopTri *mlooptri = BKE_mesh_runtime_looptri_ensure(mesh); + int looptri_len = BKE_mesh_runtime_looptri_len(mesh); + + int looptri_mask_active_len = -1; + BLI_bitmap *looptri_mask = nullptr; + if (bvh_cache_type == BVHTREE_FROM_LOOPTRI_NO_HIDDEN) { + looptri_mask = looptri_no_hidden_map_get( + mesh->mpoly, looptri_len, &looptri_mask_active_len); } - else { - /* Setup BVHTreeFromMesh */ - const MLoopTri *mlooptri = BKE_mesh_runtime_looptri_ensure(mesh); - bvhtree_from_mesh_looptri_setup_data( - data, tree, true, mesh->mvert, false, mesh->mloop, false, mlooptri, false); + + data->tree = bvhtree_from_mesh_looptri_ex(nullptr, + mesh->mvert, + false, + mesh->mloop, + false, + mlooptri, + looptri_len, + false, + looptri_mask, + looptri_mask_active_len, + 0.0, + tree_type, + 6, + bvh_cache_type, + bvh_cache_p, + mesh_eval_mutex); + + if (looptri_mask != nullptr) { + MEM_freeN(looptri_mask); } - break; + } break; case BVHTREE_FROM_EM_VERTS: case BVHTREE_FROM_EM_EDGES: case BVHTREE_FROM_EM_LOOPTRI: @@ -1570,24 +1564,20 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, break; } - if (data->tree != nullptr) { + /* Even if nullptr, the tree has been added to the cache. */ + data->cached = true; + #ifdef DEBUG + if (data->tree != nullptr) { if (BLI_bvhtree_get_tree_type(data->tree) != tree_type) { printf("tree_type %d obtained instead of %d\n", BLI_bvhtree_get_tree_type(data->tree), tree_type); } -#endif - BLI_assert(data->cached); - } - else { - free_bvhtree_from_mesh(data); - memset(data, 0, sizeof(*data)); } +#endif - data->vert_normals = BKE_mesh_vertex_normals_ensure(mesh); - - return tree; + return data->tree; } BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data, @@ -1597,80 +1587,52 @@ BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data, BVHCache **bvh_cache_p, ThreadMutex *mesh_eval_mutex) { - BVHTree *tree = nullptr; - bool is_cached = false; - - memset(data, 0, sizeof(*data)); + bvhtree_from_editmesh_setup_data(nullptr, bvh_cache_type, em, data); if (bvh_cache_p) { - is_cached = bvhcache_find(bvh_cache_p, bvh_cache_type, &tree, nullptr, nullptr); + data->cached = bvhcache_find(bvh_cache_p, bvh_cache_type, &data->tree, nullptr, nullptr); - if (is_cached && tree == nullptr) { - return tree; + if (data->cached) { + return data->tree; } } - data->tree = tree; - data->em = em; - data->cached = is_cached; switch (bvh_cache_type) { case BVHTREE_FROM_EM_VERTS: - if (is_cached == false) { - tree = bvhtree_from_editmesh_verts_ex(data, - em, - nullptr, - -1, - 0.0f, - tree_type, - 6, - bvh_cache_type, - bvh_cache_p, - mesh_eval_mutex); - } - else { - data->nearest_callback = nullptr; - data->raycast_callback = editmesh_verts_spherecast; - } + data->tree = bvhtree_from_editmesh_verts_ex(nullptr, + em, + nullptr, + -1, + 0.0f, + tree_type, + 6, + bvh_cache_type, + bvh_cache_p, + mesh_eval_mutex); break; - case BVHTREE_FROM_EM_EDGES: - if (is_cached == false) { - tree = bvhtree_from_editmesh_edges_ex(data, - em, - nullptr, - -1, - 0.0f, - tree_type, - 6, - bvh_cache_type, - bvh_cache_p, - mesh_eval_mutex); - } - else { - /* Setup #BVHTreeFromMesh */ - data->nearest_callback = nullptr; /* TODO */ - data->raycast_callback = nullptr; /* TODO */ - } + data->tree = bvhtree_from_editmesh_edges_ex(nullptr, + em, + nullptr, + -1, + 0.0f, + tree_type, + 6, + bvh_cache_type, + bvh_cache_p, + mesh_eval_mutex); break; - case BVHTREE_FROM_EM_LOOPTRI: - if (is_cached == false) { - tree = bvhtree_from_editmesh_looptri_ex(data, - em, - nullptr, - -1, - 0.0f, - tree_type, - 6, - bvh_cache_type, - bvh_cache_p, - mesh_eval_mutex); - } - else { - /* Setup #BVHTreeFromMesh */ - data->nearest_callback = editmesh_looptri_nearest_point; - data->raycast_callback = editmesh_looptri_spherecast; - } + data->tree = bvhtree_from_editmesh_looptri_ex(nullptr, + em, + nullptr, + -1, + 0.0f, + tree_type, + 6, + bvh_cache_type, + bvh_cache_p, + mesh_eval_mutex); break; case BVHTREE_FROM_VERTS: case BVHTREE_FROM_EDGES: @@ -1684,22 +1646,20 @@ BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data, break; } - if (data->tree != nullptr) { + /* Even if the tree is nullptr, it has been added to the cache if available. */ + data->cached = bvh_cache_p != nullptr; + #ifdef DEBUG + if (data->tree != nullptr) { if (BLI_bvhtree_get_tree_type(data->tree) != tree_type) { printf("tree_type %d obtained instead of %d\n", BLI_bvhtree_get_tree_type(data->tree), tree_type); } -#endif - BLI_assert(data->cached); - } - else { - free_bvhtree_from_editmesh(data); - memset(data, 0, sizeof(*data)); } +#endif - return tree; + return data->tree; } /** \} */ |