From b931dd7acaf7fd4ab0527c46703585e7f7a9fc02 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 2 May 2023 12:40:12 +0200 Subject: [PATCH 1/2] fix --- source/blender/blenlib/intern/kdtree_impl.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/source/blender/blenlib/intern/kdtree_impl.h b/source/blender/blenlib/intern/kdtree_impl.h index 53a3ea90285..bc6fd58906a 100644 --- a/source/blender/blenlib/intern/kdtree_impl.h +++ b/source/blender/blenlib/intern/kdtree_impl.h @@ -32,6 +32,7 @@ struct KDTree { KDTreeNode *nodes; uint nodes_len; uint root; + int max_node_index; #ifdef DEBUG bool is_balanced; /* ensure we call balance first */ uint nodes_len_capacity; /* max size of the tree */ @@ -90,6 +91,7 @@ KDTree *BLI_kdtree_nd_(new)(uint nodes_len_capacity) tree->nodes = MEM_mallocN(sizeof(KDTreeNode) * nodes_len_capacity, "KDTreeNode"); tree->nodes_len = 0; tree->root = KD_NODE_ROOT_IS_INIT; + tree->max_node_index = -1; #ifdef DEBUG tree->is_balanced = false; @@ -125,6 +127,7 @@ void BLI_kdtree_nd_(insert)(KDTree *tree, int index, const float co[KD_DIMS]) copy_vn_vn(node->co, co); node->index = index; node->d = 0; + tree->max_node_index = MAX2(tree->max_node_index, index); #ifdef DEBUG tree->is_balanced = false; @@ -796,12 +799,14 @@ finally: * Use when we want to loop over nodes ordered by index. * Requires indices to be aligned with nodes. */ -static uint *kdtree_order(const KDTree *tree) +static int *kdtree_order(const KDTree *tree) { const KDTreeNode *nodes = tree->nodes; - uint *order = MEM_mallocN(sizeof(uint) * tree->nodes_len, __func__); + const size_t bytes_num = sizeof(int) * (size_t)(tree->max_node_index + 1); + int *order = MEM_mallocN(bytes_num, __func__); + memset(order, -1, bytes_num); for (uint i = 0; i < tree->nodes_len; i++) { - order[nodes[i].index] = i; + order[nodes[i].index] = (int)i; } return order; } @@ -885,9 +890,12 @@ int BLI_kdtree_nd_(calc_duplicates_fast)(const KDTree *tree, }; if (use_index_order) { - uint *order = kdtree_order(tree); + int *order = kdtree_order(tree); for (uint i = 0; i < tree->nodes_len; i++) { - const uint node_index = order[i]; + const int node_index = order[i]; + if (node_index == -1) { + continue; + } const int index = (int)i; if (ELEM(duplicates[index], -1, index)) { p.search = index; -- 2.30.2 From f3a484cc2fb3b051dbf3724490c02f0913017a11 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 2 May 2023 14:16:09 +0200 Subject: [PATCH 2/2] fix --- source/blender/blenlib/intern/kdtree_impl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/blender/blenlib/intern/kdtree_impl.h b/source/blender/blenlib/intern/kdtree_impl.h index bc6fd58906a..f43e32ced76 100644 --- a/source/blender/blenlib/intern/kdtree_impl.h +++ b/source/blender/blenlib/intern/kdtree_impl.h @@ -891,12 +891,12 @@ int BLI_kdtree_nd_(calc_duplicates_fast)(const KDTree *tree, if (use_index_order) { int *order = kdtree_order(tree); - for (uint i = 0; i < tree->nodes_len; i++) { + for (int i = 0; i < tree->max_node_index + 1; i++) { const int node_index = order[i]; if (node_index == -1) { continue; } - const int index = (int)i; + const int index = i; if (ELEM(duplicates[index], -1, index)) { p.search = index; copy_vn_vn(p.search_co, tree->nodes[node_index].co); -- 2.30.2