Cleanup: Use const mesh to ensure BVH and triangulation cache

As noted in a comment now, these functions only update a cache, so they
don't change the logical state of the mesh, which is "it will have the
data when necessary." Using a const argument will help const correctness
when accessing an object's evaluated mesh.
This commit is contained in:
Hans Goudey 2021-07-02 11:37:01 -05:00
parent a1609340b4
commit 5f8969bb4b
8 changed files with 19 additions and 14 deletions

View File

@ -218,7 +218,7 @@ BVHTree *bvhtree_from_mesh_looptri_ex(struct BVHTreeFromMesh *data,
ThreadMutex *mesh_eval_mutex);
BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
struct Mesh *mesh,
const struct Mesh *mesh,
const BVHCacheType bvh_cache_type,
const int tree_type);

View File

@ -45,7 +45,7 @@ void BKE_mesh_runtime_reset(struct Mesh *mesh);
void BKE_mesh_runtime_reset_on_copy(struct Mesh *mesh, const int flag);
int BKE_mesh_runtime_looptri_len(const struct Mesh *mesh);
void BKE_mesh_runtime_looptri_recalc(struct Mesh *mesh);
const struct MLoopTri *BKE_mesh_runtime_looptri_ensure(struct Mesh *mesh);
const struct MLoopTri *BKE_mesh_runtime_looptri_ensure(const struct Mesh *mesh);
bool BKE_mesh_runtime_ensure_edit_data(struct Mesh *mesh);
bool BKE_mesh_runtime_clear_edit_data(struct Mesh *mesh);
bool BKE_mesh_runtime_reset_edit_data(struct Mesh *mesh);

View File

@ -1438,9 +1438,12 @@ static BLI_bitmap *looptri_no_hidden_map_get(const MPoly *mpoly,
/**
* 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,
struct Mesh *mesh,
const struct Mesh *mesh,
const BVHCacheType bvh_cache_type,
const int tree_type)
{
@ -1448,7 +1451,7 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
BVHCache **bvh_cache_p = (BVHCache **)&mesh->runtime.bvh_cache;
ThreadMutex *mesh_eval_mutex = (ThreadMutex *)mesh->runtime.eval_mutex;
bool is_cached = bvhcache_find(bvh_cache_p, bvh_cache_type, &tree, nullptr, nullptr);
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));

View File

@ -158,8 +158,12 @@ static void mesh_runtime_looptri_recalc_isolated(void *userdata)
BKE_mesh_runtime_looptri_recalc(mesh);
}
/* This is a ported copy of dm_getLoopTriArray(dm). */
const MLoopTri *BKE_mesh_runtime_looptri_ensure(Mesh *mesh)
/**
* \note This function only fills a cache, and therefore the mesh argument can
* be considered logically const. Concurrent access is protected by a mutex.
* \note This is a ported copy of dm_getLoopTriArray(dm).
*/
const MLoopTri *BKE_mesh_runtime_looptri_ensure(const Mesh *mesh)
{
ThreadMutex *mesh_eval_mutex = (ThreadMutex *)mesh->runtime.eval_mutex;
BLI_mutex_lock(mesh_eval_mutex);

View File

@ -143,8 +143,7 @@ static bool bvh_from_mesh(const Mesh *target_mesh,
break;
}
/* This only updates a cache and can be considered to be logically const. */
BKE_bvhtree_from_mesh_get(&r_tree_data_mesh, const_cast<Mesh *>(target_mesh), bvh_type, 2);
BKE_bvhtree_from_mesh_get(&r_tree_data_mesh, target_mesh, bvh_type, 2);
if (r_tree_data_mesh.tree == nullptr) {
return false;
}

View File

@ -162,7 +162,7 @@ static void get_closest_mesh_points(const Mesh &mesh,
{
BLI_assert(mesh.totvert > 0);
BVHTreeFromMesh tree_data;
BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(&mesh), BVHTREE_FROM_VERTS, 2);
BKE_bvhtree_from_mesh_get(&tree_data, &mesh, BVHTREE_FROM_VERTS, 2);
get_closest_in_bvhtree(tree_data, positions, r_point_indices, r_distances_sq, r_positions);
free_bvhtree_from_mesh(&tree_data);
}
@ -175,7 +175,7 @@ static void get_closest_mesh_edges(const Mesh &mesh,
{
BLI_assert(mesh.totedge > 0);
BVHTreeFromMesh tree_data;
BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(&mesh), BVHTREE_FROM_EDGES, 2);
BKE_bvhtree_from_mesh_get(&tree_data, &mesh, BVHTREE_FROM_EDGES, 2);
get_closest_in_bvhtree(tree_data, positions, r_edge_indices, r_distances_sq, r_positions);
free_bvhtree_from_mesh(&tree_data);
}
@ -188,7 +188,7 @@ static void get_closest_mesh_looptris(const Mesh &mesh,
{
BLI_assert(mesh.totpoly > 0);
BVHTreeFromMesh tree_data;
BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(&mesh), BVHTREE_FROM_LOOPTRI, 2);
BKE_bvhtree_from_mesh_get(&tree_data, &mesh, BVHTREE_FROM_LOOPTRI, 2);
get_closest_in_bvhtree(tree_data, positions, r_looptri_indices, r_distances_sq, r_positions);
free_bvhtree_from_mesh(&tree_data);
}

View File

@ -83,8 +83,7 @@ static float3 normal_to_euler_rotation(const float3 normal)
static Span<MLoopTri> get_mesh_looptris(const Mesh &mesh)
{
/* This only updates a cache and can be considered to be logically const. */
const MLoopTri *looptris = BKE_mesh_runtime_looptri_ensure(const_cast<Mesh *>(&mesh));
const MLoopTri *looptris = BKE_mesh_runtime_looptri_ensure(&mesh);
const int looptris_len = BKE_mesh_runtime_looptri_len(&mesh);
return {looptris, looptris_len};
}

View File

@ -95,7 +95,7 @@ static void raycast_to_mesh(const Mesh *mesh,
BLI_assert(ray_origins.size() == r_hit_distances.size() || r_hit_distances.is_empty());
BVHTreeFromMesh tree_data;
BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(mesh), BVHTREE_FROM_LOOPTRI, 4);
BKE_bvhtree_from_mesh_get(&tree_data, mesh, BVHTREE_FROM_LOOPTRI, 4);
if (tree_data.tree != nullptr) {
for (const int i : ray_origins.index_range()) {