diff options
Diffstat (limited to 'source/blender/blenkernel/intern/bvhutils.cc')
-rw-r--r-- | source/blender/blenkernel/intern/bvhutils.cc | 111 |
1 files changed, 46 insertions, 65 deletions
diff --git a/source/blender/blenkernel/intern/bvhutils.cc b/source/blender/blenkernel/intern/bvhutils.cc index 707201207d9..5e7a4eea0cd 100644 --- a/source/blender/blenkernel/intern/bvhutils.cc +++ b/source/blender/blenkernel/intern/bvhutils.cc @@ -125,9 +125,9 @@ bool bvhcache_has_tree(const BVHCache *bvh_cache, const BVHTree *tree) return false; } -BVHCache *bvhcache_init(void) +BVHCache *bvhcache_init() { - BVHCache *cache = (BVHCache *)MEM_callocN(sizeof(BVHCache), __func__); + BVHCache *cache = MEM_cnew<BVHCache>(__func__); BLI_mutex_init(&cache->mutex); return cache; } @@ -147,9 +147,6 @@ static void bvhcache_insert(BVHCache *bvh_cache, BVHTree *tree, BVHCacheType typ item->is_filled = true; } -/** - * frees a bvhcache - */ void bvhcache_free(BVHCache *bvh_cache) { for (int index = 0; index < BVHTREE_MAX_ITEM; index++) { @@ -161,9 +158,11 @@ void bvhcache_free(BVHCache *bvh_cache) MEM_freeN(bvh_cache); } -/* BVH tree balancing inside a mutex lock must be run in isolation. Balancing +/** + * BVH-tree balancing inside a mutex lock must be run in isolation. Balancing * is multithreaded, and we do not want the current thread to start another task - * that may involve acquiring the same mutex lock that it is waiting for. */ + * that may involve acquiring the same mutex lock that it is waiting for. + */ static void bvhtree_balance_isolated(void *userdata) { BLI_bvhtree_balance((BVHTree *)userdata); @@ -182,6 +181,7 @@ static void bvhtree_balance(BVHTree *tree, const bool isolate) } /** \} */ + /* -------------------------------------------------------------------- */ /** \name Local Callbacks * \{ */ @@ -233,8 +233,12 @@ float bvhtree_sphereray_tri_intersection(const BVHTreeRay *ray, * BVH from meshes callbacks */ -/* Callback to bvh tree nearest point. The tree must have been built using bvhtree_from_mesh_faces. - * userdata must be a BVHMeshCallbackUserdata built from the same mesh as the tree. */ +/** + * Callback to BVH-tree nearest point. + * The tree must have been built using #bvhtree_from_mesh_faces. + * + * \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree. + */ static void mesh_faces_nearest_point(void *userdata, int index, const float co[3], @@ -325,8 +329,12 @@ static void editmesh_looptri_nearest_point(void *userdata, } } -/* Callback to bvh tree raycast. The tree must have been built using bvhtree_from_mesh_faces. - * userdata must be a BVHMeshCallbackUserdata built from the same mesh as the tree. */ +/** + * Callback to BVH-tree ray-cast. + * The tree must have been built using bvhtree_from_mesh_faces. + * + * \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree. + */ static void mesh_faces_spherecast(void *userdata, int index, const BVHTreeRay *ray, @@ -430,8 +438,12 @@ static void editmesh_looptri_spherecast(void *userdata, } } -/* Callback to bvh tree nearest point. The tree must have been built using bvhtree_from_mesh_edges. - * userdata must be a BVHMeshCallbackUserdata built from the same mesh as the tree. */ +/** + * Callback to BVH-tree nearest point. + * The tree must have been built using #bvhtree_from_mesh_edges. + * + * \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree. + */ static void mesh_edges_nearest_point(void *userdata, int index, const float co[3], @@ -491,8 +503,12 @@ static void editmesh_verts_spherecast(void *userdata, mesh_verts_spherecast_do(index, eve->co, ray, hit); } -/* Callback to bvh tree raycast. The tree must have been built using bvhtree_from_mesh_verts. - * userdata must be a BVHMeshCallbackUserdata built from the same mesh as the tree. */ +/** + * Callback to BVH-tree ray-cast. + * The tree must have been built using bvhtree_from_mesh_verts. + * + * \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree. + */ static void mesh_verts_spherecast(void *userdata, int index, const BVHTreeRay *ray, @@ -504,8 +520,12 @@ static void mesh_verts_spherecast(void *userdata, mesh_verts_spherecast_do(index, v, ray, hit); } -/* Callback to bvh tree raycast. The tree must have been built using bvhtree_from_mesh_edges. - * userdata must be a BVHMeshCallbackUserdata built from the same mesh as the tree. */ +/** + * Callback to BVH-tree ray-cast. + * The tree must have been built using bvhtree_from_mesh_edges. + * + * \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree. + */ static void mesh_edges_spherecast(void *userdata, int index, const BVHTreeRay *ray, @@ -647,7 +667,6 @@ static void bvhtree_from_mesh_verts_setup_data(BVHTreeFromMesh *data, data->vert_allocated = vert_allocated; } -/* Builds a bvh tree where nodes are the vertices of the given em */ BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data, BMEditMesh *em, const BLI_bitmap *verts_mask, @@ -703,13 +722,6 @@ BVHTree *bvhtree_from_editmesh_verts( data, em, nullptr, -1, epsilon, tree_type, axis, BVHTREE_FROM_VERTS, nullptr, nullptr); } -/** - * Builds a bvh tree where nodes are the given vertices (NOTE: does not copy given `vert`!). - * \param vert_allocated: if true, vert freeing will be done when freeing data. - * \param verts_mask: if not null, true elements give which vert to add to BVH tree. - * \param verts_num_active: if >= 0, number of active verts to add to BVH tree - * (else will be computed from mask). - */ BVHTree *bvhtree_from_mesh_verts_ex(BVHTreeFromMesh *data, const MVert *vert, const int verts_num, @@ -860,7 +872,6 @@ static void bvhtree_from_mesh_edges_setup_data(BVHTreeFromMesh *data, data->edge_allocated = edge_allocated; } -/* Builds a bvh tree where nodes are the edges of the given em */ BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data, BMEditMesh *em, const BLI_bitmap *edges_mask, @@ -915,14 +926,6 @@ BVHTree *bvhtree_from_editmesh_edges( data, em, nullptr, -1, epsilon, tree_type, axis, BVHTREE_FROM_VERTS, nullptr, nullptr); } -/** - * Builds a bvh tree where nodes are the given edges . - * \param vert, vert_allocated: if true, elem freeing will be done when freeing data. - * \param edge, edge_allocated: if true, elem freeing will be done when freeing data. - * \param edges_mask: if not null, true elements give which vert to add to BVH tree. - * \param edges_num_active: if >= 0, number of active edges to add to BVH tree - * (else will be computed from mask). - */ BVHTree *bvhtree_from_mesh_edges_ex(BVHTreeFromMesh *data, const MVert *vert, const bool vert_allocated, @@ -1049,15 +1052,6 @@ static void bvhtree_from_mesh_faces_setup_data(BVHTreeFromMesh *data, data->face_allocated = face_allocated; } -/** - * Builds a bvh tree where nodes are the given tessellated faces - * (NOTE: does not copy given mfaces!). - * \param vert_allocated: if true, vert freeing will be done when freeing data. - * \param face_allocated: if true, face freeing will be done when freeing data. - * \param faces_mask: if not null, true elements give which faces to add to BVH tree. - * \param faces_num_active: if >= 0, number of active faces to add to BVH tree - * (else will be computed from mask). - */ BVHTree *bvhtree_from_mesh_faces_ex(BVHTreeFromMesh *data, const MVert *vert, const bool vert_allocated, @@ -1135,7 +1129,7 @@ static BVHTree *bvhtree_from_editmesh_looptri_create_tree(float epsilon, if (tree) { const BMLoop *(*looptris)[3] = (const BMLoop *(*)[3])em->looptris; - /* Insert BMesh-tessellation triangles into the bvh tree, unless they are hidden + /* Insert BMesh-tessellation triangles into the BVH-tree, unless they are hidden * and/or selected. Even if the faces themselves are not selected for the snapped * transform, having a vertex selected means the face (and thus it's tessellated * triangles) will be moving and will not be a good snap targets. */ @@ -1231,9 +1225,6 @@ static void bvhtree_from_mesh_looptri_setup_data(BVHTreeFromMesh *data, data->looptri_allocated = looptri_allocated; } -/** - * Builds a bvh tree where nodes are the looptri faces of the given bm - */ BVHTree *bvhtree_from_editmesh_looptri_ex(BVHTreeFromEditMesh *data, BMEditMesh *em, const BLI_bitmap *looptri_mask, @@ -1288,11 +1279,6 @@ BVHTree *bvhtree_from_editmesh_looptri( data, em, nullptr, -1, epsilon, tree_type, axis, BVHTREE_FROM_VERTS, nullptr, nullptr); } -/** - * Builds a BVH-tree where nodes are the looptri faces of the given mesh. - * - * \note for edit-mesh this is currently a duplicate of #bvhtree_from_mesh_faces_ex - */ BVHTree *bvhtree_from_mesh_looptri_ex(BVHTreeFromMesh *data, const struct MVert *vert, const bool vert_allocated, @@ -1435,12 +1421,6 @@ static BLI_bitmap *looptri_no_hidden_map_get(const MPoly *mpoly, return looptri_mask; } -/** - * Builds or queries a bvhcache for the cache bvhtree of the request type. - * - * \note This function only fills a cache, and therefore the mesh argument can - * be considered logically const. Concurrent access is protected by a mutex. - */ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, const struct Mesh *mesh, const BVHCacheType bvh_cache_type, @@ -1621,12 +1601,11 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, memset(data, 0, sizeof(*data)); } + data->vert_normals = BKE_mesh_vertex_normals_ensure(mesh); + return tree; } -/** - * Builds or queries a bvhcache for the cache bvhtree of the request type. - */ BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data, struct BMEditMesh *em, const int tree_type, @@ -1684,7 +1663,7 @@ BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data, mesh_eval_mutex); } else { - /* Setup BVHTreeFromMesh */ + /* Setup #BVHTreeFromMesh */ data->nearest_callback = nullptr; /* TODO */ data->raycast_callback = nullptr; /* TODO */ } @@ -1704,7 +1683,7 @@ BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data, mesh_eval_mutex); } else { - /* Setup BVHTreeFromMesh */ + /* Setup #BVHTreeFromMesh */ data->nearest_callback = editmesh_looptri_nearest_point; data->raycast_callback = editmesh_looptri_spherecast; } @@ -1741,7 +1720,10 @@ BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data, /** \} */ -/* Frees data allocated by a call to bvhtree_from_editmesh_*. */ +/* -------------------------------------------------------------------- */ +/** \name Free Functions + * \{ */ + void free_bvhtree_from_editmesh(struct BVHTreeFromEditMesh *data) { if (data->tree) { @@ -1752,7 +1734,6 @@ void free_bvhtree_from_editmesh(struct BVHTreeFromEditMesh *data) } } -/* Frees data allocated by a call to bvhtree_from_mesh_*. */ void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data) { if (data->tree && !data->cached) { |