From 2752b8a400bdebc19a6c90a5c7a86d9e2b71b893 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 3 Oct 2025 11:36:04 +1000 Subject: [PATCH] BLI_kdtree: use the index as a tie breaker when finding duplicates Use the index as a tie breaker for deterministic results with the "Merge by Distance" operator. Ref !147252 --- source/blender/blenlib/intern/kdtree_impl.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/blender/blenlib/intern/kdtree_impl.h b/source/blender/blenlib/intern/kdtree_impl.h index 86021852e9c..d19d910737d 100644 --- a/source/blender/blenlib/intern/kdtree_impl.h +++ b/source/blender/blenlib/intern/kdtree_impl.h @@ -940,7 +940,10 @@ int BLI_kdtree_nd_(calc_duplicates_cb)(const KDTree *tree, else if (target_index != neighbor_index) { float &dist_sq_best = duplicates_dist_sq[neighbor_index]; /* Steal the target if it's closer. */ - if (dist_sq < dist_sq_best) { + if ((dist_sq < dist_sq_best) || + /* Pick the lowest index as a tie breaker for a deterministic result. */ + ((dist_sq == dist_sq_best) && (node_index < target_index))) + { dist_sq_best = dist_sq; duplicates[neighbor_index] = node_index; }