Skip to content

Commit

Permalink
Making KDTree.remove() handle cases where multiple elements share the…
Browse files Browse the repository at this point in the history
… same spatial location
  • Loading branch information
LuizZak committed Jul 23, 2024
1 parent 7335b50 commit 536213a
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,20 @@ public struct KDTree<Element: KDTreeLocatable> where Element.Vector: VectorCompa
return
}

let nearest = root.nearestSubdivision(to: element.location)
guard nearest.0.element == element else {
return
}
var allNearest: [Subdivision] = []
root.nearestSubdivisions(
to: element.location,
distanceSquared: .zero,
onMatch: { if $0.element == element { allNearest.append($0) } }
)

guard let path = root.findPath(to: nearest.0) else {
return
}
for nearest in allNearest {
guard let path = root.findPath(to: nearest) else {
return
}

remove(at: .init(path: path))
remove(at: .init(path: path))
}
}

/// Removes an element at a given index in this k-d tree.
Expand Down

0 comments on commit 536213a

Please sign in to comment.