Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2014-01-03 18:56:02 +0400
committerCampbell Barton <ideasman42@gmail.com>2014-01-04 03:44:02 +0400
commitfd6ef46d6d63929cf4f386a729872be8829bf5ab (patch)
tree367a46d4d887a84c7fd83179c6af2e56cad93635 /source/blender
parent1af82c0194e3247e04c56c6db977df10c089c6dd (diff)
KDTree: ensure balance runs before usage (in debug mode)
Diffstat (limited to 'source/blender')
-rw-r--r--source/blender/blenlib/intern/BLI_kdtree.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/source/blender/blenlib/intern/BLI_kdtree.c b/source/blender/blenlib/intern/BLI_kdtree.c
index 57227dc5d5f..c0e7c21420a 100644
--- a/source/blender/blenlib/intern/BLI_kdtree.c
+++ b/source/blender/blenlib/intern/BLI_kdtree.c
@@ -44,6 +44,9 @@ struct KDTree {
KDTreeNode *nodes;
unsigned int totnode;
KDTreeNode *root;
+#ifdef DEBUG
+ bool is_balanced; /* ensure we call balance first */
+#endif
};
#define KD_STACK_INIT 100 /* initial size for array (on the stack) */
@@ -62,6 +65,10 @@ KDTree *BLI_kdtree_new(unsigned int maxsize)
tree->totnode = 0;
tree->root = NULL;
+#ifdef DEBUG
+ tree->is_balanced = false;
+#endif
+
return tree;
}
@@ -92,6 +99,10 @@ void BLI_kdtree_insert(KDTree *tree, int index, const float co[3], const float n
node->index = index;
node->d = 0;
+
+#ifdef DEBUG
+ tree->is_balanced = false;
+#endif
}
static KDTreeNode *kdtree_balance(KDTreeNode *nodes, unsigned int totnode, unsigned int axis)
@@ -144,6 +155,10 @@ static KDTreeNode *kdtree_balance(KDTreeNode *nodes, unsigned int totnode, unsig
void BLI_kdtree_balance(KDTree *tree)
{
tree->root = kdtree_balance(tree->nodes, tree->totnode, 0);
+
+#ifdef DEBUG
+ tree->is_balanced = true;
+#endif
}
static float squared_distance(const float v2[3], const float v1[3], const float UNUSED(n1[3]), const float n2[3])
@@ -188,6 +203,10 @@ int BLI_kdtree_find_nearest(KDTree *tree, const float co[3], const float nor[3],
float min_dist, cur_dist;
unsigned int totstack, cur = 0;
+#ifdef DEBUG
+ BLI_assert(tree->is_balanced == true);
+#endif
+
if (!tree->root)
return -1;
@@ -298,6 +317,10 @@ int BLI_kdtree_find_nearest_n(KDTree *tree, const float co[3], const float nor[3
unsigned int totstack, cur = 0;
unsigned int i, found = 0;
+#ifdef DEBUG
+ BLI_assert(tree->is_balanced == true);
+#endif
+
if (!tree->root || n == 0)
return 0;
@@ -416,7 +439,11 @@ int BLI_kdtree_range_search(KDTree *tree, const float co[3], const float nor[3],
float range2 = range * range, dist2;
unsigned int totstack, cur = 0, found = 0, totfoundstack = 0;
- if (!tree || !tree->root)
+#ifdef DEBUG
+ BLI_assert(tree->is_balanced == true);
+#endif
+
+ if (!tree->root)
return 0;
stack = defaultstack;