Fix #109885: Check if BVH tree is null in correct place

The `BKE_bvhtree_from_pointcloud_get` function have requirements for
input point cloud argument and initialization of `BVHTreeFromPointCloud`
can be skipped. Due to `BVHTreeFromPointCloud` is not initialized by
default constructor, it can contains garbage data. To check if tree is
initialized field of `BVHTreeFromPointCloud`, return argument shouldn't
be ignored. `[[nodiscard]]` attributes is added.

Pull Request: https://projects.blender.org/blender/blender/pulls/109892
This commit is contained in:
Iliya Katueshenock 2023-07-10 18:12:41 +02:00 committed by Hans Goudey
parent 5c4694759b
commit a8186e1542
4 changed files with 18 additions and 11 deletions

@ -247,9 +247,11 @@ typedef struct BVHTreeFromPointCloud {
const float (*coords)[3]; const float (*coords)[3];
} BVHTreeFromPointCloud; } BVHTreeFromPointCloud;
BVHTree *BKE_bvhtree_from_pointcloud_get(struct BVHTreeFromPointCloud *data, #ifdef __cplusplus
const struct PointCloud *pointcloud, [[nodiscard]] BVHTree *BKE_bvhtree_from_pointcloud_get(BVHTreeFromPointCloud *data,
int tree_type); const PointCloud *pointcloud,
int tree_type);
#endif
void free_bvhtree_from_pointcloud(struct BVHTreeFromPointCloud *data); void free_bvhtree_from_pointcloud(struct BVHTreeFromPointCloud *data);

@ -1369,9 +1369,9 @@ void free_bvhtree_from_mesh(BVHTreeFromMesh *data)
/** \name Point Cloud BVH Building /** \name Point Cloud BVH Building
* \{ */ * \{ */
BVHTree *BKE_bvhtree_from_pointcloud_get(BVHTreeFromPointCloud *data, [[nodiscard]] BVHTree *BKE_bvhtree_from_pointcloud_get(BVHTreeFromPointCloud *data,
const PointCloud *pointcloud, const PointCloud *pointcloud,
const int tree_type) const int tree_type)
{ {
int tot_point = pointcloud->totpoint; int tot_point = pointcloud->totpoint;
BVHTree *tree = bvhtree_new_common(0.0f, tree_type, 6, tot_point, tot_point); BVHTree *tree = bvhtree_new_common(0.0f, tree_type, 6, tot_point, tot_point);

@ -95,8 +95,8 @@ static bool calculate_pointcloud_proximity(const VArray<float3> &positions,
MutableSpan<float3> r_locations) MutableSpan<float3> r_locations)
{ {
BVHTreeFromPointCloud bvh_data; BVHTreeFromPointCloud bvh_data;
BKE_bvhtree_from_pointcloud_get(&bvh_data, &pointcloud, 2); const BVHTree *tree = BKE_bvhtree_from_pointcloud_get(&bvh_data, &pointcloud, 2);
if (bvh_data.tree == nullptr) { if (tree == nullptr) {
return false; return false;
} }

@ -72,14 +72,19 @@ static void node_init(bNodeTree * /*tree*/, bNode *node)
static void get_closest_pointcloud_points(const PointCloud &pointcloud, static void get_closest_pointcloud_points(const PointCloud &pointcloud,
const VArray<float3> &positions, const VArray<float3> &positions,
const IndexMask &mask, const IndexMask &mask,
const MutableSpan<int> r_indices, MutableSpan<int> r_indices,
const MutableSpan<float> r_distances_sq) MutableSpan<float> r_distances_sq)
{ {
BLI_assert(positions.size() >= r_indices.size()); BLI_assert(positions.size() >= r_indices.size());
BLI_assert(pointcloud.totpoint > 0); BLI_assert(pointcloud.totpoint > 0);
BVHTreeFromPointCloud tree_data; BVHTreeFromPointCloud tree_data;
BKE_bvhtree_from_pointcloud_get(&tree_data, &pointcloud, 2); const BVHTree *tree = BKE_bvhtree_from_pointcloud_get(&tree_data, &pointcloud, 2);
if (tree == nullptr) {
r_indices.fill(0);
r_distances_sq.fill(0.0f);
return;
}
mask.foreach_index([&](const int i) { mask.foreach_index([&](const int i) {
BVHTreeNearest nearest; BVHTreeNearest nearest;